You are here

protected function GroupEvaluator::applyContexts in Block Visibility Groups 8

Apply contexts.

Parameters

\Drupal\Core\Condition\ConditionPluginCollection $conditions: A collection of condition plugins.

string $logic: The logical operator.

Return value

bool Whether the conditions have been applied or not.

1 call to GroupEvaluator::applyContexts()
GroupEvaluator::evaluateGroup in src/GroupEvaluator.php
Evaluate Block Visibility Group.

File

src/GroupEvaluator.php, line 85

Class

GroupEvaluator
Class ConditionEvaluator.

Namespace

Drupal\block_visibility_groups

Code

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;
}