ContextManager.php in Context 8
File
src/ContextManager.php
View source
<?php
namespace Drupal\context;
use Drupal\context\Entity\Context;
use Drupal\context\Plugin\ContextReaction\Blocks;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Condition\ConditionAccessResolverTrait;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Theme\ThemeManagerInterface;
class ContextManager {
use ConditionAccessResolverTrait;
use StringTranslationTrait;
protected $entityQuery;
protected $entityManager;
protected $contextRepository;
protected $contextHandler;
protected $contextConditionsEvaluated = FALSE;
protected $activeContexts = [];
private $entityFormBuilder;
protected $themeManager;
function __construct(QueryFactory $entityQuery, EntityManagerInterface $entityManager, ContextRepositoryInterface $contextRepository, ContextHandlerInterface $contextHandler, EntityFormBuilderInterface $entityFormBuilder, ThemeManagerInterface $themeManager) {
$this->entityQuery = $entityQuery;
$this->entityManager = $entityManager;
$this->contextRepository = $contextRepository;
$this->contextHandler = $contextHandler;
$this->entityFormBuilder = $entityFormBuilder;
$this->themeManager = $themeManager;
}
public function getContexts() {
$contextIds = $this->entityQuery
->get('context')
->execute();
$contexts = $this->entityManager
->getStorage('context')
->loadMultiple($contextIds);
uasort($contexts, [
$this,
'sortContextsByWeight',
]);
return $contexts;
}
public function getContextsByGroup() {
$contexts = $this
->getContexts();
$groups = [];
foreach ($contexts as $context_id => $context) {
$group = $context
->getGroup();
if ($group === Context::CONTEXT_GROUP_NONE) {
$group = 'not_grouped';
}
$groups[$group][$context_id] = $context;
}
return $groups;
}
public function contextExists($name) {
$entity = $this->entityQuery
->get('context')
->condition('name', $name)
->execute();
return (bool) $entity;
}
public function conditionsHasBeenEvaluated() {
return $this->contextConditionsEvaluated;
}
public function getActiveContexts() {
if ($this
->conditionsHasBeenEvaluated()) {
return $this->activeContexts;
}
$this
->evaluateContexts();
return $this->activeContexts;
}
public function evaluateContexts() {
foreach ($this
->getContexts() as $context) {
if ($this
->evaluateContextConditions($context) && !$context
->disabled()) {
$this->activeContexts[] = $context;
}
}
$this->contextConditionsEvaluated = TRUE;
}
public function getActiveReactions($reactionType = NULL) {
$reactions = [];
foreach ($this
->getActiveContexts() as $context) {
if (is_null($reactionType)) {
foreach ($context
->getReactions() as $reaction) {
if ($reaction instanceof Blocks) {
$blocks = $reaction
->getBlocks();
$current_theme = $this
->getCurrentTheme();
foreach ($blocks as $block) {
if ($block
->getConfiguration()['theme'] == $current_theme) {
$reactions[] = $reaction;
break;
}
}
}
else {
$reactions[] = $reaction;
}
}
continue;
}
$contextReactions = $context
->getReactions();
foreach ($contextReactions as $reaction) {
if (class_exists($reactionType) && $reaction instanceof $reactionType) {
$reactions[] = $reaction;
continue;
}
if ($reaction
->getPluginId() === $reactionType) {
$reactions[] = $reaction;
continue;
}
}
}
return $reactions;
}
public function evaluateContextConditions(ContextInterface $context) {
$conditions = $context
->getConditions();
$this
->applyContexts($conditions);
$logic = $context
->requiresAllConditions() ? 'and' : 'or';
if (!count($conditions)) {
$logic = 'and';
}
return $this
->resolveConditions($conditions, $logic);
}
protected function applyContexts(ConditionPluginCollection &$conditions) {
foreach ($conditions as $condition) {
if ($condition instanceof ContextAwarePluginInterface) {
try {
$contexts = $this->contextRepository
->getRuntimeContexts(array_values($condition
->getContextMapping()));
$this->contextHandler
->applyContextMapping($condition, $contexts);
} catch (ContextException $e) {
return FALSE;
}
}
}
return TRUE;
}
public function getForm(ContextInterface $context, $formType = 'edit', array $form_state_additions = array()) {
return $this->entityFormBuilder
->getForm($context, $formType, $form_state_additions);
}
public function sortContextsByWeight(ContextInterface $a, ContextInterface $b) {
if ($a
->getWeight() == $b
->getWeight()) {
return 0;
}
return $a
->getWeight() < $b
->getWeight() ? -1 : 1;
}
private function getCurrentTheme() {
return $this->themeManager
->getActiveTheme()
->getName();
}
}
Classes
Name |
Description |
ContextManager |
This is the manager service for the context module and should not be
confused with the built in contexts in Drupal. |