class GroupEvaluator in Block Visibility Groups 8
Class ConditionEvaluator.
@package Drupal\block_visibility_groups
Hierarchy
- class \Drupal\block_visibility_groups\GroupEvaluator implements GroupEvaluatorInterface uses ConditionAccessResolverTrait
Expanded class hierarchy of GroupEvaluator
2 files declare their use of GroupEvaluator
- ConditionGroup.php in src/
Plugin/ Condition/ ConditionGroup.php - GroupInfo.php in block_visibility_groups_admin/
src/ GroupInfo.php
1 string reference to 'GroupEvaluator'
1 service uses GroupEvaluator
File
- src/
GroupEvaluator.php, line 18
Namespace
Drupal\block_visibility_groupsView source
class GroupEvaluator implements GroupEvaluatorInterface {
use ConditionAccessResolverTrait;
/**
* The plugin context handler.
*
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
*/
protected $contextHandler;
/**
* The context manager service.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* A list of group evaluations.
*
* @var array
*/
protected $group_evaluations = [];
/**
* Constructor.
*/
public function __construct(ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository) {
$this->contextRepository = $context_repository;
$this->contextHandler = $context_handler;
}
/**
* {@inheritdoc}
*/
public function evaluateGroup(BlockVisibilityGroup $block_visibility_group) {
$group_id = $block_visibility_group
->id();
if (!isset($this->group_evaluations[$group_id])) {
/** @var \Drupal\Core\Condition\ConditionPluginCollection $conditions */
$conditions = $block_visibility_group
->getConditions();
if (empty($conditions)) {
// If no conditions then always true.
return TRUE;
}
$logic = $block_visibility_group
->getLogic();
if ($this
->applyContexts($conditions, $logic)) {
$this->group_evaluations[$group_id] = $this
->resolveConditions($conditions, $logic);
}
else {
$this->group_evaluations[$group_id] = FALSE;
}
}
return $this->group_evaluations[$group_id];
}
/**
* Apply contexts.
*
* @param \Drupal\Core\Condition\ConditionPluginCollection $conditions
* A collection of condition plugins.
* @param string $logic
* The logical operator.
*
* @return bool
* Whether the conditions have been applied or not.
*/
protected function applyContexts(ConditionPluginCollection &$conditions, $logic) {
$have_1_testable_condition = FALSE;
foreach ($conditions as $id => $condition) {
if ($condition instanceof ContextAwarePluginInterface) {
try {
$contexts = $this->contextRepository
->getRuntimeContexts(array_values($condition
->getContextMapping()));
$this->contextHandler
->applyContextMapping($condition, $contexts);
$have_1_testable_condition = TRUE;
} catch (ContextException $e) {
if ($logic == 'and') {
// Logic is all and found condition with contextException.
return FALSE;
}
$conditions
->removeInstanceId($id);
}
}
else {
$have_1_testable_condition = TRUE;
}
}
if ($logic == 'or' && !$have_1_testable_condition) {
return FALSE;
}
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConditionAccessResolverTrait:: |
protected | function | Resolves the given conditions based on the condition logic ('and'/'or'). | |
GroupEvaluator:: |
protected | property | The plugin context handler. | |
GroupEvaluator:: |
protected | property | The context manager service. | |
GroupEvaluator:: |
protected | property | A list of group evaluations. | |
GroupEvaluator:: |
protected | function | Apply contexts. | |
GroupEvaluator:: |
public | function |
Evaluate Block Visibility Group. Overrides GroupEvaluatorInterface:: |
|
GroupEvaluator:: |
public | function | Constructor. |