You are here

class GinToolbarActiveTrail in Gin Toolbar 8

Class GinToolbarActiveTrail.

@package Drupal\gin_toolbar\Menu

Hierarchy

Expanded class hierarchy of GinToolbarActiveTrail

1 string reference to 'GinToolbarActiveTrail'
gin_toolbar.services.yml in ./gin_toolbar.services.yml
gin_toolbar.services.yml
1 service uses GinToolbarActiveTrail
gin_toolbar.active_trail in ./gin_toolbar.services.yml
Drupal\gin_toolbar\Menu\GinToolbarActiveTrail

File

src/Menu/GinToolbarActiveTrail.php, line 12

Namespace

Drupal\gin_toolbar\Menu
View source
class GinToolbarActiveTrail extends MenuActiveTrail {

  /**
   * {@inheritdoc}
   *
   * Change the active trail for node add/edit/view routes.
   */
  protected function doGetActiveTrailIds($menu_name) {
    $route_name = $this->routeMatch
      ->getRouteName();
    $route_params = $this->routeMatch
      ->getRawParameters()
      ->all();
    if ($route_name === 'node.add') {
      $link = $this
        ->getLinkByRoutes($menu_name, [
        [
          $route_name,
          $route_params,
        ],
        [
          'node.add_page',
          [],
        ],
        [
          'system.admin_content',
          [],
        ],
      ]);
    }
    if ($route_name === 'node.add_page') {
      $link = $this
        ->getLinkByRoutes($menu_name, [
        [
          $route_name,
          $route_params,
        ],
        [
          'system.admin_content',
          [],
        ],
      ]);
    }
    if (in_array($route_name, [
      'entity.node.canonical',
      'entity.node.edit_form',
    ])) {
      $link = $this
        ->getLinkByRoutes($menu_name, [
        [
          $route_name,
          $route_params,
        ],
        [
          'system.admin_content',
          [],
        ],
      ]);
    }
    if (!isset($link)) {
      return parent::doGetActiveTrailIds($menu_name);
    }
    $active_trail = [
      '' => '',
    ];
    if ($parents = $this->menuLinkManager
      ->getParentIds($link
      ->getPluginId())) {
      $active_trail = $parents + $active_trail;
    }
    return $active_trail;
  }

  /**
   * {@inheritdoc}
   *
   * The active trail logic is different here, so the active trails should be
   * cached separately.
   */
  protected function getCid() {
    if (!isset($this->cid)) {
      $this->cid = 'gin-toolbar-' . parent::getCid();
    }
    return $this->cid;
  }

  /**
   * Get a possible link to base the active trail on.
   *
   * @param string $menu_name
   *   The name of the menu.
   * @param array $routes
   *   An array of route name & route params combinations in order of relevance.
   */
  protected function getLinkByRoutes(string $menu_name, array $routes) {
    foreach ($routes as $route) {
      [
        $route_name,
        $route_params,
      ] = $route;
      $links = $this->menuLinkManager
        ->loadLinksByRoute($route_name, $route_params, $menu_name);
      foreach ($links as $link) {
        if (!empty($link
          ->getParent())) {
          return $link;
        }
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheCollector::$cache protected property The cache backend that should be used. 1
CacheCollector::$cacheCreated protected property Stores the cache creation time.
CacheCollector::$cacheInvalidated protected property Flag that indicates of the cache has been invalidated.
CacheCollector::$cacheLoaded protected property Indicates if the collected cache was already loaded.
CacheCollector::$cid protected property The cache id that is used for the cache entry.
CacheCollector::$keysToPersist protected property An array of keys to add to the cache on service termination.
CacheCollector::$keysToRemove protected property An array of keys to remove from the cache on service termination.
CacheCollector::$lock protected property The lock backend that should be used. 1
CacheCollector::$storage protected property Storage for the data itself.
CacheCollector::$tags protected property A list of tags that are used for the cache entry.
CacheCollector::clear public function Clears the collected cache entry. Overrides CacheCollectorInterface::clear 1
CacheCollector::delete public function Deletes the element. Overrides CacheCollectorInterface::delete
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::get public function Gets value from the cache. Overrides CacheCollectorInterface::get 2
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 1
CacheCollector::invalidateCache protected function Invalidate the cache.
CacheCollector::lazyLoadCache protected function Loads the cache if not already done. 1
CacheCollector::normalizeLockName protected function Normalizes a cache ID in order to comply with database limitations.
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Resets the local cache. Overrides CacheCollectorInterface::reset 1
CacheCollector::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface::set 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1
GinToolbarActiveTrail::doGetActiveTrailIds protected function Change the active trail for node add/edit/view routes. Overrides MenuActiveTrail::doGetActiveTrailIds
GinToolbarActiveTrail::getCid protected function The active trail logic is different here, so the active trails should be cached separately. Overrides MenuActiveTrail::getCid
GinToolbarActiveTrail::getLinkByRoutes protected function Get a possible link to base the active trail on.
MenuActiveTrail::$menuLinkManager protected property The menu link plugin manager.
MenuActiveTrail::$routeMatch protected property The route match object for the current page.
MenuActiveTrail::getActiveLink public function Fetches a menu link which matches the route name, parameters and menu name. Overrides MenuActiveTrailInterface::getActiveLink
MenuActiveTrail::getActiveTrailIds public function This implementation caches all active trail IDs per route match for *all* menus whose active trails are calculated on that page. This ensures 1 cache get for all active trails per page load, rather than N. Overrides MenuActiveTrailInterface::getActiveTrailIds
MenuActiveTrail::resolveCacheMiss protected function Overrides CacheCollector::resolveCacheMiss
MenuActiveTrail::__construct public function Constructs a \Drupal\Core\Menu\MenuActiveTrail object. Overrides CacheCollector::__construct