You are here

class GroupEvaluator in Block Visibility Groups 8

Class ConditionEvaluator.

@package Drupal\block_visibility_groups

Hierarchy

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'
block_visibility_groups.services.yml in ./block_visibility_groups.services.yml
block_visibility_groups.services.yml
1 service uses GroupEvaluator
block_visibility_groups.group_evaluator in ./block_visibility_groups.services.yml
Drupal\block_visibility_groups\GroupEvaluator

File

src/GroupEvaluator.php, line 18

Namespace

Drupal\block_visibility_groups
View 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

Namesort descending Modifiers Type Description Overrides
ConditionAccessResolverTrait::resolveConditions protected function Resolves the given conditions based on the condition logic ('and'/'or').
GroupEvaluator::$contextHandler protected property The plugin context handler.
GroupEvaluator::$contextRepository protected property The context manager service.
GroupEvaluator::$group_evaluations protected property A list of group evaluations.
GroupEvaluator::applyContexts protected function Apply contexts.
GroupEvaluator::evaluateGroup public function Evaluate Block Visibility Group. Overrides GroupEvaluatorInterface::evaluateGroup
GroupEvaluator::__construct public function Constructor.