From b5360d91be24b0186af0c254c163c6788471431e Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 4 Oct 2021 07:53:55 -0400 Subject: [PATCH] Add an index of reports at /report. --- .../core/report/farm_report.links.menu.yml | 4 + .../core/report/farm_report.permissions.yml | 2 + modules/core/report/farm_report.routing.yml | 7 ++ .../src/Controller/ReportController.php | 97 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 modules/core/report/farm_report.links.menu.yml create mode 100644 modules/core/report/farm_report.permissions.yml create mode 100644 modules/core/report/farm_report.routing.yml create mode 100644 modules/core/report/src/Controller/ReportController.php diff --git a/modules/core/report/farm_report.links.menu.yml b/modules/core/report/farm_report.links.menu.yml new file mode 100644 index 00000000..7120e261 --- /dev/null +++ b/modules/core/report/farm_report.links.menu.yml @@ -0,0 +1,4 @@ +farm.report: + title: Reports + parent: system.admin + route_name: farm.report diff --git a/modules/core/report/farm_report.permissions.yml b/modules/core/report/farm_report.permissions.yml new file mode 100644 index 00000000..1384bb33 --- /dev/null +++ b/modules/core/report/farm_report.permissions.yml @@ -0,0 +1,2 @@ +access farm report index: + title: 'Access report index' diff --git a/modules/core/report/farm_report.routing.yml b/modules/core/report/farm_report.routing.yml new file mode 100644 index 00000000..25ae3007 --- /dev/null +++ b/modules/core/report/farm_report.routing.yml @@ -0,0 +1,7 @@ +farm.report: + path: '/report' + defaults: + _controller: '\Drupal\farm_report\Controller\ReportController::index' + _title: 'Reports' + requirements: + _permission: 'access farm report index' diff --git a/modules/core/report/src/Controller/ReportController.php b/modules/core/report/src/Controller/ReportController.php new file mode 100644 index 00000000..33d111ee --- /dev/null +++ b/modules/core/report/src/Controller/ReportController.php @@ -0,0 +1,97 @@ +menuLinkTree = $menu_link_tree; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('menu.link_tree') + ); + } + + /** + * The index of reports. + * + * @return array + * Returns a render array. + */ + public function index(): array { + + // Load all menu links below it. + $parameters = new MenuTreeParameters(); + $parameters->setRoot('farm.report')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks(); + $tree = $this->menuLinkTree->load(NULL, $parameters); + $manipulators = [ + ['callable' => 'menu.default_tree_manipulators:checkAccess'], + ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], + ]; + $tree = $this->menuLinkTree->transform($tree, $manipulators); + $tree_access_cacheability = new CacheableMetadata(); + $links = []; + foreach ($tree as $element) { + $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access)); + + // Only render accessible links. + if (!$element->access->isAllowed()) { + continue; + } + + // Include the link. + $links[] = $element->link; + } + if (!empty($links)) { + $items = []; + foreach ($links as $link) { + $items[] = [ + 'title' => $link->getTitle(), + 'description' => $link->getDescription(), + 'url' => $link->getUrlObject(), + ]; + } + $output = [ + '#theme' => 'admin_block_content', + '#content' => $items, + ]; + } + else { + $output = [ + '#markup' => $this->t('You do not have any reports.'), + ]; + } + return $output; + } + +}