You are here

public function ContextHandler::getMatchingContexts in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Plugin/Context/ContextHandler.php \Drupal\Core\Plugin\Context\ContextHandler::getMatchingContexts()

Determines which contexts satisfy the constraints of a given definition.

@todo Use context definition objects after https://www.drupal.org/node/2281635.

Parameters

\Drupal\Component\Plugin\Context\ContextInterface[] $contexts: An array of contexts.

\Drupal\Core\Plugin\Context\ContextDefinitionInterface $definition: The definition to satisfy.

Return value

\Drupal\Component\Plugin\Context\ContextInterface[] An array of matching contexts.

Overrides ContextHandlerInterface::getMatchingContexts

1 call to ContextHandler::getMatchingContexts()
ContextHandler::checkRequirements in core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
Checks a set of requirements against a set of contexts.

File

core/lib/Drupal/Core/Plugin/Context/ContextHandler.php, line 49
Contains \Drupal\Core\Plugin\Context\ContextHandler.

Class

ContextHandler
Provides methods to handle sets of contexts.

Namespace

Drupal\Core\Plugin\Context

Code

public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) {
  return array_filter($contexts, function (ContextInterface $context) use ($definition) {
    $context_definition = $context
      ->getContextDefinition();

    // If the data types do not match, this context is invalid unless the
    // expected data type is any, which means all data types are supported.
    if ($definition
      ->getDataType() != 'any' && $definition
      ->getDataType() != $context_definition
      ->getDataType()) {
      return FALSE;
    }

    // If any constraint does not match, this context is invalid.
    foreach ($definition
      ->getConstraints() as $constraint_name => $constraint) {
      if ($context_definition
        ->getConstraint($constraint_name) != $constraint) {
        return FALSE;
      }
    }

    // All contexts with matching data type and contexts are valid.
    return TRUE;
  });
}