You are here

class ContextActiveTrail in Context Active Trail 8.2

Same name and namespace in other branches
  1. 8 src/ContextActiveTrail.php \Drupal\context_active_trail\ContextActiveTrail

Allow the active trail to be set manually.

Hierarchy

Expanded class hierarchy of ContextActiveTrail

1 file declares its use of ContextActiveTrail
context_active_trail.module in ./context_active_trail.module
Hook implementations for context_active_trail.

File

src/ContextActiveTrail.php, line 16

Namespace

Drupal\context_active_trail
View source
class ContextActiveTrail extends MenuActiveTrail {

  /**
   * Cache tag for things that are dependent on the breadcrumbs configuration.
   *
   * Whenever something might change which breadcrumb builder is responsible for
   * a given breadcrumb, this tag needs to be invalidated.
   */
  const CACHE_TAG_BREADCRUMBS = "cache_tag_breadcrumbs";

  /**
   * The context manager.
   *
   * @var \Drupal\Context\ContextManager
   */
  protected $contextManager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   *   The menu link plugin manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   A route match object for finding the active link.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
   * @param \Drupal\Context\ContextManager $context_manager
   *   The context manager.
   */
  public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock, ContextManager $context_manager) {
    parent::__construct($menu_link_manager, $route_match, $cache, $lock);
    $this->contextManager = $context_manager;
    $this->tags[] = 'context_active_trail';
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveLink($menu_name = NULL) {

    // Try to get the value from context.
    foreach ($this->contextManager
      ->getActiveReactions('active_trail') as $reaction) {
      if ($link_id = $reaction
        ->getLinkId()) {
        try {
          $instance = $this->menuLinkManager
            ->getInstance([
            'id' => $link_id,
          ]);
        } catch (PluginNotFoundException $e) {
          \Drupal::logger('context_active_trail')
            ->error('Could not find the configured menu link to set active: @error', [
            '@error' => $e
              ->getMessage(),
          ]);
          return FALSE;
        }

        // Check that the instance is in the menu.
        if (!empty($menu_name) && $instance
          ->getMenuName() != $menu_name) {
          break;
        }
        return $instance;
      }
    }

    // Fall back to the default.
    return parent::getActiveLink($menu_name);
  }

}

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
ContextActiveTrail::$contextManager protected property The context manager.
ContextActiveTrail::CACHE_TAG_BREADCRUMBS constant Cache tag for things that are dependent on the breadcrumbs configuration.
ContextActiveTrail::getActiveLink public function Fetches a menu link which matches the route name, parameters and menu name. Overrides MenuActiveTrail::getActiveLink
ContextActiveTrail::__construct public function Constructor. Overrides MenuActiveTrail::__construct
MenuActiveTrail::$menuLinkManager protected property The menu link plugin manager.
MenuActiveTrail::$routeMatch protected property The route match object for the current page.
MenuActiveTrail::doGetActiveTrailIds protected function Helper method for ::getActiveTrailIds().
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::getCid protected function Overrides CacheCollector::getCid
MenuActiveTrail::resolveCacheMiss protected function Overrides CacheCollector::resolveCacheMiss