You are here

class ContextReaction in Breadcrumb Manager 8

Class ContextReaction.

@BreadcrumbTitleResolver( id = "context_reaction", label = @Translation("Context Reaction"), description = @Translation("Resolve title from a Context reaction."), weight = 0, enabled = FALSE )

@package Drupal\breadcrumb_manager\Plugin\BreadcrumbTitleResolver

Hierarchy

Expanded class hierarchy of ContextReaction

File

modules/breadcrumb_manager_context/src/Plugin/BreadcrumbTitleResolver/ContextReaction.php, line 28

Namespace

Drupal\breadcrumb_manager_context\Plugin\BreadcrumbTitleResolver
View source
class ContextReaction extends BreadcrumbTitleResolverBase {

  /**
   * The Module Handler Service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

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

  /**
   * The Request Stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Breadcrumb Manager cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, ContextManager $context_manager, RequestStack $request_stack, CacheBackendInterface $cache) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->moduleHandler = $module_handler;
    $this->contextManager = $context_manager;
    $this->requestStack = $request_stack;
    $this->cache = $cache;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('module_handler'), $container
      ->get('context.manager'), $container
      ->get('request_stack'), $container
      ->get('cache.breadcrumb_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function isActive() {
    if ($this->moduleHandler
      ->moduleExists('context') === FALSE) {
      return FALSE;
    }
    return parent::isActive();
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle($path, Request $request, RouteMatchInterface $route_match) {
    foreach ($this
      ->getReactions($path, $request) as $reaction) {
      $title = $reaction
        ->execute();
      if (!empty($title)) {
        return $title;
      }
    }
    return NULL;
  }

  /**
   * Get reactions.
   *
   * @param string $path
   *   The path.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The Request.
   *
   * @return \Drupal\context\ContextReactionInterface[]
   *   An array of Context reactions.
   */
  protected function getReactions($path, Request $request) {
    $cid = "context:{$path}";
    $cache = $this->cache
      ->get($cid);
    if (!empty($cache->data)) {
      return $cache->data;
    }
    $requestHasChanged = FALSE;
    $currentRequest = $this->requestStack
      ->getCurrentRequest();
    if ($currentRequest
      ->getPathInfo() !== $request
      ->getPathInfo()) {

      // Pop out the original Request.
      $orig = $this->requestStack
        ->pop();

      // Assign original session to the new Request.
      $request
        ->setSession($orig
        ->getSession());

      // Switch the new Request with the old one and evaluate contexts again.
      $this->requestStack
        ->push($request);
      $this->contextManager
        ->evaluateContexts();

      // Push the old Request back.
      $this->requestStack
        ->push($orig);

      // Mark Request as changed.
      $requestHasChanged = TRUE;
    }
    $reactions = $this->contextManager
      ->getActiveReactions('breadcrumb');
    $this->cache
      ->set($cid, $reactions, Cache::PERMANENT, [
      'breadcrumb_manager',
      'breadcrumb_manager_context',
    ]);

    // If Request has changed, evaluate contexts again in order to avoid wrong
    // reactions to be used.
    if ($requestHasChanged) {
      $this->contextManager
        ->evaluateContexts();
    }
    return $reactions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BreadcrumbTitleResolverBase::$isActive protected property 1
BreadcrumbTitleResolverBase::setActive public function Set active. Overrides BreadcrumbTitleResolverInterface::setActive
ContextReaction::$cache protected property Breadcrumb Manager cache bin.
ContextReaction::$contextManager protected property The Context Manager Service.
ContextReaction::$moduleHandler protected property The Module Handler Service.
ContextReaction::$requestStack protected property The Request Stack.
ContextReaction::create public static function Creates an instance of the plugin. Overrides BreadcrumbTitleResolverBase::create
ContextReaction::getReactions protected function Get reactions.
ContextReaction::getTitle public function Get title. Overrides BreadcrumbTitleResolverInterface::getTitle
ContextReaction::isActive public function Is active. Overrides BreadcrumbTitleResolverBase::isActive
ContextReaction::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.