Add an index of reports at /report.
This commit is contained in:
parent
dccaa1f43a
commit
b5360d91be
|
@ -0,0 +1,4 @@
|
|||
farm.report:
|
||||
title: Reports
|
||||
parent: system.admin
|
||||
route_name: farm.report
|
|
@ -0,0 +1,2 @@
|
|||
access farm report index:
|
||||
title: 'Access report index'
|
|
@ -0,0 +1,7 @@
|
|||
farm.report:
|
||||
path: '/report'
|
||||
defaults:
|
||||
_controller: '\Drupal\farm_report\Controller\ReportController::index'
|
||||
_title: 'Reports'
|
||||
requirements:
|
||||
_permission: 'access farm report index'
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_report\Controller;
|
||||
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\Menu\MenuLinkTreeInterface;
|
||||
use Drupal\Core\Menu\MenuTreeParameters;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Report controller.
|
||||
*/
|
||||
class ReportController extends ControllerBase {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* The menu link tree service.
|
||||
*
|
||||
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
|
||||
*/
|
||||
protected $menuLinkTree;
|
||||
|
||||
/**
|
||||
* Constructs a new ReportController.
|
||||
*
|
||||
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree
|
||||
* The menu link tree service.
|
||||
*/
|
||||
public function __construct(MenuLinkTreeInterface $menu_link_tree) {
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue