You are here

public function ContextAll::evaluate in Context 8.4

Evaluates the condition and returns TRUE or FALSE accordingly.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides ConditionInterface::evaluate

File

src/Plugin/Condition/ContextAll.php, line 122

Class

ContextAll
Provides a 'Context (all)' condition.

Namespace

Drupal\context\Plugin\Condition

Code

public function evaluate() {
  $required_contexts = $negated_contexts = [];
  $asterisk_context = '';
  $values = array_filter(array_map('trim', explode("\n", $this->configuration['values'])));
  if (empty($values)) {
    return TRUE;
  }
  foreach ($values as $key) {
    if (substr($key, 0, 1) == "~") {
      $negated_contexts[] = substr($key, 1);
    }
    elseif (strpos($key, '*') !== FALSE) {
      $asterisk_context = $key;
    }
    elseif (!empty($key)) {
      $required_contexts[] = $key;
    }
  }

  // Handle negated contexts first.
  foreach ($negated_contexts as $name) {

    /** @var \Drupal\context\ContextInterface $negated_context */
    $negated_context = $this->contextManager
      ->getContext($name);
    if ($this->contextManager
      ->evaluateContextConditions($negated_context) && !$negated_context
      ->disabled()) {
      return FALSE;
    }
  }

  // Now handle required contexts.
  foreach ($required_contexts as $name) {

    /** @var \Drupal\context\ContextInterface $required_context */
    if ($required_context = $this->contextManager
      ->getContext($name)) {
      if (!$this->contextManager
        ->evaluateContextConditions($required_context) && !$required_context
        ->disabled()) {
        return FALSE;
      }
    }
  }

  // Handle the asterisks/wildcard contexts.

  /** @var \Drupal\context\ContextInterface $asterisk_contexts */
  if ($asterisk_contexts = $this->contextManager
    ->getContext($asterisk_context)) {
    foreach ($asterisk_contexts as $context) {
      if (!$this->contextManager
        ->evaluateContextConditions($context) && !$context
        ->disabled()) {
        return FALSE;
      }
    }
  }
  return TRUE;
}