Standardize index building across all index controllers

This commit is contained in:
Paul Weidner 2023-10-09 11:58:50 -07:00 committed by Michael Stenta
parent b815dacb4f
commit e5c3bda5bf
5 changed files with 60 additions and 73 deletions

View File

@ -118,27 +118,21 @@ class CsvImportController extends ControllerBase {
$tree_access_cacheability = new CacheableMetadata(); $tree_access_cacheability = new CacheableMetadata();
$tree_access_cacheability->addCacheTags($this->entityTypeManager()->getStorage('migration')->getEntityType()->getListCacheTags()); $tree_access_cacheability->addCacheTags($this->entityTypeManager()->getStorage('migration')->getEntityType()->getListCacheTags());
$links = []; // Build items for each csv importer.
$items = [];
foreach ($tree as $element) { foreach ($tree as $element) {
$tree_access_cacheability->addCacheableDependency($element->access); $tree_access_cacheability->addCacheableDependency($element->access);
if ($element->access->isAllowed()) {
// Only render accessible links.
if (!$element->access->isAllowed()) {
continue;
}
// Include the link.
$links[] = $element->link;
}
if (!empty($links)) {
$items = [];
foreach ($links as $link) {
$items[] = [ $items[] = [
'title' => $link->getTitle(), 'title' => $element->link->getTitle(),
'description' => $link->getDescription(), 'description' => $element->link->getDescription(),
'url' => $link->getUrlObject(), 'url' => $element->link->getUrlObject(),
]; ];
} }
}
// Render items.
if (!empty($items)) {
$output = [ $output = [
'#theme' => 'admin_block_content', '#theme' => 'admin_block_content',
'#content' => $items, '#content' => $items,

View File

@ -63,27 +63,21 @@ class ImportController extends ControllerBase {
// Start cacheability for indexer list. // Start cacheability for indexer list.
$tree_access_cacheability = new CacheableMetadata(); $tree_access_cacheability = new CacheableMetadata();
$links = []; // Build list item for each importer.
$items = [];
foreach ($tree as $element) { foreach ($tree as $element) {
$tree_access_cacheability->addCacheableDependency($element->access); $tree_access_cacheability->addCacheableDependency($element->access);
if ($element->access->isAllowed()) {
// Only render accessible links.
if (!$element->access->isAllowed()) {
continue;
}
// Include the link.
$links[] = $element->link;
}
if (!empty($links)) {
$items = [];
foreach ($links as $link) {
$items[] = [ $items[] = [
'title' => $link->getTitle(), 'title' => $element->link->getTitle(),
'description' => $link->getDescription(), 'description' => $element->link->getDescription(),
'url' => $link->getUrlObject(), 'url' => $element->link->getUrlObject(),
]; ];
} }
}
// Render items.
if (!empty($items)) {
$output = [ $output = [
'#theme' => 'admin_block_content', '#theme' => 'admin_block_content',
'#content' => $items, '#content' => $items,

View File

@ -59,9 +59,9 @@ class QuickFormController extends ControllerBase {
$quick_forms = $this->quickFormInstanceManager->getInstances(); $quick_forms = $this->quickFormInstanceManager->getInstances();
$items = []; $items = [];
foreach ($quick_forms as $id => $quick_form) { foreach ($quick_forms as $id => $quick_form) {
$cacheability->addCacheableDependency($quick_form);
$url = Url::fromRoute('farm.quick.' . $id); $url = Url::fromRoute('farm.quick.' . $id);
if ($url->access()) { if ($url->access()) {
$cacheability->addCacheableDependency($quick_form);
$items[] = [ $items[] = [
'title' => $quick_form->getLabel(), 'title' => $quick_form->getLabel(),
'description' => $quick_form->getDescription(), 'description' => $quick_form->getDescription(),
@ -69,6 +69,8 @@ class QuickFormController extends ControllerBase {
]; ];
} }
} }
// Render items.
if (!empty($items)) { if (!empty($items)) {
$output = [ $output = [
'#theme' => 'admin_block_content', '#theme' => 'admin_block_content',

View File

@ -59,28 +59,25 @@ class ReportController extends ControllerBase {
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
]; ];
$tree = $this->menuLinkTree->transform($tree, $manipulators); $tree = $this->menuLinkTree->transform($tree, $manipulators);
// Start cacheability for report list.
$tree_access_cacheability = new CacheableMetadata(); $tree_access_cacheability = new CacheableMetadata();
$links = [];
// Build list item for each report.
$items = [];
foreach ($tree as $element) { foreach ($tree as $element) {
$tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access)); $tree_access_cacheability->addCacheableDependency($element->access);
if ($element->access->isAllowed()) {
// Only render accessible links.
if (!$element->access->isAllowed()) {
continue;
}
// Include the link.
$links[] = $element->link;
}
if (!empty($links)) {
$items = [];
foreach ($links as $link) {
$items[] = [ $items[] = [
'title' => $link->getTitle(), 'title' => $element->link->getTitle(),
'description' => $link->getDescription(), 'description' => $element->link->getDescription(),
'url' => $link->getUrlObject(), 'url' => $element->link->getUrlObject(),
]; ];
} }
}
// Render items.
if (!empty($items)) {
$output = [ $output = [
'#theme' => 'admin_block_content', '#theme' => 'admin_block_content',
'#content' => $items, '#content' => $items,
@ -91,6 +88,7 @@ class ReportController extends ControllerBase {
'#markup' => $this->t('You do not have any reports.'), '#markup' => $this->t('You do not have any reports.'),
]; ];
} }
$tree_access_cacheability->applyTo($output);
return $output; return $output;
} }

View File

@ -59,38 +59,37 @@ class SetupController extends ControllerBase {
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
]; ];
$tree = $this->menuLinkTree->transform($tree, $manipulators); $tree = $this->menuLinkTree->transform($tree, $manipulators);
// Start cacheability for setup list.
$tree_access_cacheability = new CacheableMetadata(); $tree_access_cacheability = new CacheableMetadata();
// Build list item for each setup item.
$items = []; $items = [];
foreach ($tree as $element) { foreach ($tree as $element) {
$tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access)); $tree_access_cacheability->addCacheableDependency($element->access);
if ($element->access->isAllowed()) {
// Only render accessible links. $items[] = [
if (!$element->access->isAllowed()) { 'title' => $element->link->getTitle(),
continue; 'description' => $element->link->getDescription(),
'url' => $element->link->getUrlObject(),
];
} }
}
// Include the link. // Render items.
$items[] = [ if (!empty($items)) {
'title' => $element->link->getTitle(), $output = [
'description' => $element->link->getDescription(), '#theme' => 'admin_block_content',
'url' => $element->link->getUrlObject(), '#content' => $items,
]; ];
} }
// Create render array with cacheability.
$render = [];
$tree_access_cacheability->applyTo($render);
// Add message if there are no setup items.
if (empty($items)) {
$render['#markup'] = $this->t('You do not have any setup items.');
}
else { else {
$render['#theme'] = 'admin_block_content'; $output = [
$render['#content'] = $items; '#markup' => $this->t('You do not have any setup items.'),
];
} }
$tree_access_cacheability->applyTo($output);
return $render; return $output;
} }
} }