class ModuleAdminLinksHelper

Same name in other branches
  1. 11.x core/modules/system/src/ModuleAdminLinksHelper.php \Drupal\system\ModuleAdminLinksHelper

Provides a helper for generating module admin links.

Hierarchy

Expanded class hierarchy of ModuleAdminLinksHelper

2 files declare their use of ModuleAdminLinksHelper
AdminController.php in core/modules/system/src/Controller/AdminController.php
HelpController.php in core/modules/help/src/Controller/HelpController.php
1 string reference to 'ModuleAdminLinksHelper'
system.services.yml in core/modules/system/system.services.yml
core/modules/system/system.services.yml
1 service uses ModuleAdminLinksHelper
system.module_admin_links_helper in core/modules/system/system.services.yml
Drupal\system\ModuleAdminLinksHelper

File

core/modules/system/src/ModuleAdminLinksHelper.php, line 14

Namespace

Drupal\system
View source
class ModuleAdminLinksHelper {
    
    /**
     * The cache key for the menu tree.
     */
    const ADMIN_LINKS_MENU_TREE = 'admin_links_menu_tree';
    
    /**
     * Constructs a new service instance.
     *
     * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
     *   The menu link tree service.
     * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memoryCache
     *   The memory cache.
     */
    public function __construct(MenuLinkTreeInterface $menuLinkTree, MemoryCacheInterface $memoryCache) {
    }
    
    /**
     * Generates a list of admin tasks offered by a specified module.
     *
     * @param string $module
     *   The module name.
     *
     * @return array
     *   An array of task links.
     */
    public function getModuleAdminLinks(string $module) : array {
        // Cache the menu tree as it is expensive to load.
        
        /** @var \Drupal\Core\Menu\MenuLinkTreeElement[]|false $menuTree */
        $cacheItem = $this->memoryCache
            ->get(self::ADMIN_LINKS_MENU_TREE);
        if ($cacheItem) {
            $menuTree = $cacheItem->data;
        }
        else {
            $parameters = (new MenuTreeParameters())->setRoot('system.admin')
                ->excludeRoot()
                ->onlyEnabledLinks();
            $menuTree = $this->menuLinkTree
                ->load('system.admin', $parameters);
            $manipulators = [
                [
                    'callable' => 'menu.default_tree_manipulators:checkAccess',
                ],
                [
                    'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
                ],
                [
                    'callable' => 'menu.default_tree_manipulators:flatten',
                ],
            ];
            $menuTree = $this->menuLinkTree
                ->transform($menuTree, $manipulators);
            $this->memoryCache
                ->set(self::ADMIN_LINKS_MENU_TREE, $menuTree);
        }
        $admin_tasks = [];
        foreach ($menuTree as $element) {
            if (!$element->access
                ->isAllowed()) {
                // @todo Bubble cacheability metadata of both accessible and
                //   inaccessible links. Currently made impossible by the way admin
                //   tasks are rendered. See https://www.drupal.org/node/2488958
                continue;
            }
            $link = $element->link;
            if ($link->getProvider() !== $module) {
                continue;
            }
            $admin_tasks[] = [
                'title' => $link->getTitle(),
                'description' => $link->getDescription(),
                'url' => $link->getUrlObject(),
            ];
        }
        return $admin_tasks;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ModuleAdminLinksHelper::ADMIN_LINKS_MENU_TREE constant The cache key for the menu tree.
ModuleAdminLinksHelper::getModuleAdminLinks public function Generates a list of admin tasks offered by a specified module.
ModuleAdminLinksHelper::__construct public function Constructs a new service instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.