ContextReaction.php in Breadcrumb Manager 8
File
modules/breadcrumb_manager_context/src/Plugin/BreadcrumbTitleResolver/ContextReaction.php
View source
<?php
namespace Drupal\breadcrumb_manager_context\Plugin\BreadcrumbTitleResolver;
use Drupal\breadcrumb_manager\Plugin\BreadcrumbTitleResolverBase;
use Drupal\context\ContextManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class ContextReaction extends BreadcrumbTitleResolverBase {
protected $moduleHandler;
protected $contextManager;
protected $requestStack;
protected $cache;
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;
}
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'));
}
public function isActive() {
if ($this->moduleHandler
->moduleExists('context') === FALSE) {
return FALSE;
}
return parent::isActive();
}
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;
}
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()) {
$orig = $this->requestStack
->pop();
$request
->setSession($orig
->getSession());
$this->requestStack
->push($request);
$this->contextManager
->evaluateContexts();
$this->requestStack
->push($orig);
$requestHasChanged = TRUE;
}
$reactions = $this->contextManager
->getActiveReactions('breadcrumb');
$this->cache
->set($cid, $reactions, Cache::PERMANENT, [
'breadcrumb_manager',
'breadcrumb_manager_context',
]);
if ($requestHasChanged) {
$this->contextManager
->evaluateContexts();
}
return $reactions;
}
}