You are here

class MenuTrailByPathActiveTrail in Menu Trail By Path 8

Overrides the class for the file entity normalizer from HAL.

Hierarchy

Expanded class hierarchy of MenuTrailByPathActiveTrail

2 files declare their use of MenuTrailByPathActiveTrail
MenuTrailByPathActiveTrailHtmlClassTest.php in tests/src/Functional/MenuTrailByPathActiveTrailHtmlClassTest.php
MenuTrailByPathSettingsForm.php in src/Form/MenuTrailByPathSettingsForm.php

File

src/MenuTrailByPathActiveTrail.php, line 20

Namespace

Drupal\menu_trail_by_path
View source
class MenuTrailByPathActiveTrail extends MenuActiveTrail {

  /**
   * Disabled menu trail.
   */
  const MENU_TRAIL_DISABLED = 'disabled';

  /**
   * Menu trail is created using this module.
   */
  const MENU_TRAIL_PATH = 'path';

  /**
   * Menu trail is created by Drupal Core.
   */
  const MENU_TRAIL_CORE = 'core';

  /**
   * @var \Drupal\menu_trail_by_path\Path\PathHelperInterface
   */
  protected $pathHelper;

  /**
   * @var \Drupal\menu_trail_by_path\Menu\MenuHelperInterface
   */
  protected $menuHelper;

  /**
   * @var \Drupal\Core\Routing\RequestContext
   */
  protected $context;

  /**
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The configuration object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * MenuTrailByPathActiveTrail constructor.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   * @param \Drupal\menu_trail_by_path\Path\PathHelperInterface $path_helper
   * @param \Drupal\menu_trail_by_path\Menu\MenuHelperInterface $menu_helper
   * @param \Drupal\Core\Routing\RequestContext $context
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   */
  public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock, PathHelperInterface $path_helper, MenuHelperInterface $menu_helper, RequestContext $context, LanguageManagerInterface $languageManager, ConfigFactoryInterface $config_factory) {
    parent::__construct($menu_link_manager, $route_match, $cache, $lock);
    $this->pathHelper = $path_helper;
    $this->menuHelper = $menu_helper;
    $this->context = $context;
    $this->languageManager = $languageManager;
    $this->config = $config_factory
      ->get('menu_trail_by_path.settings');
  }

  /**
   * {@inheritdoc}
   *
   * @see https://www.drupal.org/node/2824594
   */
  protected function getCid() {
    if (!isset($this->cid)) {
      return parent::getCid() . ":langcode:{$this->languageManager->getCurrentLanguage()->getId()}:pathinfo:{$this->context->getPathInfo()}";
    }
    return $this->cid;
  }

  /**
   * {@inheritdoc}
   */
  protected function doGetActiveTrailIds($menu_name) {

    // Parent ids; used both as key and value to ensure uniqueness.
    // We always want all the top-level links with parent == ''.
    $active_trail = array(
      '' => '',
    );
    $entity = Menu::load($menu_name);
    if (!$entity) {
      return $active_trail;
    }

    // Build an active trail based on the trail source setting.
    $trail_source = $entity
      ->getThirdPartySetting('menu_trail_by_path', 'trail_source') ?: $this->config
      ->get('trail_source');
    if ($trail_source == static::MENU_TRAIL_CORE) {
      return parent::doGetActiveTrailIds($menu_name);
    }
    elseif ($trail_source == static::MENU_TRAIL_DISABLED) {
      return $active_trail;
    }

    // If a link in the given menu indeed matches the path, then use it to
    // complete the active trail.
    if ($active_link = $this
      ->getActiveTrailLink($menu_name)) {
      if ($parents = $this->menuLinkManager
        ->getParentIds($active_link
        ->getPluginId())) {
        $active_trail = $parents + $active_trail;
      }
    }
    return $active_trail;
  }

  /**
   * Fetches the deepest, heaviest menu link which matches the deepest trail path url.
   *
   * @param string $menu_name
   *   The menu within which to find the active trail link.
   *
   * @return \Drupal\Core\Menu\MenuLinkInterface|NULL
   *   The menu link for the given menu, or NULL if there is no matching menu link.
   */
  public function getActiveTrailLink($menu_name) {
    $menu_links = $this->menuHelper
      ->getMenuLinks($menu_name);
    $trail_urls = $this->pathHelper
      ->getUrls();
    foreach (array_reverse($trail_urls) as $trail_url) {
      foreach (array_reverse($menu_links) as $menu_link) {

        /* @var $menu_link \Drupal\Core\Menu\MenuLinkInterface */

        /* @var $trail_url \Drupal\Core\Url */
        if ($menu_link
          ->getUrlObject()
          ->toString() == $trail_url
          ->toString()) {
          return $menu_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
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
MenuTrailByPathActiveTrail::$config protected property The configuration object.
MenuTrailByPathActiveTrail::$context protected property
MenuTrailByPathActiveTrail::$languageManager protected property
MenuTrailByPathActiveTrail::$menuHelper protected property
MenuTrailByPathActiveTrail::$pathHelper protected property
MenuTrailByPathActiveTrail::doGetActiveTrailIds protected function Helper method for ::getActiveTrailIds(). Overrides MenuActiveTrail::doGetActiveTrailIds
MenuTrailByPathActiveTrail::getActiveTrailLink public function Fetches the deepest, heaviest menu link which matches the deepest trail path url.
MenuTrailByPathActiveTrail::getCid protected function Overrides MenuActiveTrail::getCid
MenuTrailByPathActiveTrail::MENU_TRAIL_CORE constant Menu trail is created by Drupal Core.
MenuTrailByPathActiveTrail::MENU_TRAIL_DISABLED constant Disabled menu trail.
MenuTrailByPathActiveTrail::MENU_TRAIL_PATH constant Menu trail is created using this module.
MenuTrailByPathActiveTrail::__construct public function MenuTrailByPathActiveTrail constructor. Overrides MenuActiveTrail::__construct