ImportController.php in farmOS 2.x
File
modules/core/import/src/Controller/ImportController.php
View source
<?php
namespace Drupal\farm_import\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;
class ImportController extends ControllerBase {
use StringTranslationTrait;
protected $menuLinkTree;
public function __construct(MenuLinkTreeInterface $menu_link_tree) {
$this->menuLinkTree = $menu_link_tree;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('menu.link_tree'));
}
public function index() : array {
$parameters = new MenuTreeParameters();
$parameters
->setRoot('farm.import')
->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));
if (!$element->access
->isAllowed()) {
continue;
}
$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 importers.'),
];
}
return $output;
}
}