You are here

public function ContextHandler::filterPluginDefinitionsByContexts in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Plugin/Context/ContextHandler.php \Drupal\Core\Plugin\Context\ContextHandler::filterPluginDefinitionsByContexts()
  2. 9 core/lib/Drupal/Core/Plugin/Context/ContextHandler.php \Drupal\Core\Plugin\Context\ContextHandler::filterPluginDefinitionsByContexts()

Determines plugins whose constraints are satisfied by a set of contexts.

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

Parameters

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

array $definitions: An array of plugin definitions.

Return value

array An array of plugin definitions.

Overrides ContextHandlerInterface::filterPluginDefinitionsByContexts

File

core/lib/Drupal/Core/Plugin/Context/ContextHandler.php, line 19

Class

ContextHandler
Provides methods to handle sets of contexts.

Namespace

Drupal\Core\Plugin\Context

Code

public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
  $checked_requirements = [];
  return array_filter($definitions, function ($plugin_definition) use ($contexts, &$checked_requirements) {
    $context_definitions = $this
      ->getContextDefinitions($plugin_definition);
    if ($context_definitions) {

      // Generate a unique key for the current context definitions. This will
      // allow calling checkRequirements() once for all plugins that have the
      // same context definitions.
      $context_definitions_key = hash('sha256', serialize($context_definitions));
      if (!isset($checked_requirements[$context_definitions_key])) {

        // Check the set of contexts against the requirements.
        $checked_requirements[$context_definitions_key] = $this
          ->checkRequirements($contexts, $context_definitions);
      }
      return $checked_requirements[$context_definitions_key];
    }

    // If this plugin doesn't need any context, it is available to use.
    return TRUE;
  });
}