3 namespace Drupal\Core\Plugin\Context;
5 use Drupal\Component\Plugin\Exception\ContextException;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Plugin\ContextAwarePluginInterface;
10 * Provides methods to handle sets of contexts.
12 class ContextHandler implements ContextHandlerInterface {
17 public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
18 return array_filter($definitions, function ($plugin_definition) use ($contexts) {
19 // If this plugin doesn't need any context, it is available to use.
20 if (!isset($plugin_definition['context'])) {
24 // Check the set of contexts against the requirements.
25 return $this->checkRequirements($contexts, $plugin_definition['context']);
32 public function checkRequirements(array $contexts, array $requirements) {
33 foreach ($requirements as $requirement) {
34 if ($requirement->isRequired() && !$this->getMatchingContexts($contexts, $requirement)) {
44 public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) {
45 return array_filter($contexts, function (ContextInterface $context) use ($definition) {
46 $context_definition = $context->getContextDefinition();
48 // If the data types do not match, this context is invalid unless the
49 // expected data type is any, which means all data types are supported.
50 if ($definition->getDataType() != 'any' && $definition->getDataType() != $context_definition->getDataType()) {
54 // If any constraint does not match, this context is invalid.
55 foreach ($definition->getConstraints() as $constraint_name => $constraint) {
56 if ($context_definition->getConstraint($constraint_name) != $constraint) {
61 // All contexts with matching data type and contexts are valid.
69 public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = []) {
70 /** @var $contexts \Drupal\Core\Plugin\Context\ContextInterface[] */
71 $mappings += $plugin->getContextMapping();
72 // Loop through each of the expected contexts.
76 foreach ($plugin->getContextDefinitions() as $plugin_context_id => $plugin_context_definition) {
77 // If this context was given a specific name, use that.
78 $context_id = isset($mappings[$plugin_context_id]) ? $mappings[$plugin_context_id] : $plugin_context_id;
79 if (!empty($contexts[$context_id])) {
80 // This assignment has been used, remove it.
81 unset($mappings[$plugin_context_id]);
83 // Plugins have their on context objects, only the value is applied.
84 // They also need to know about the cacheability metadata of where that
85 // value is coming from, so pass them through to those objects.
86 $plugin_context = $plugin->getContext($plugin_context_id);
87 if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) {
88 $plugin_context->addCacheableDependency($contexts[$context_id]);
91 // Pass the value to the plugin if there is one.
92 if ($contexts[$context_id]->hasContextValue()) {
93 $plugin->setContextValue($plugin_context_id, $contexts[$context_id]->getContextData());
95 elseif ($plugin_context_definition->isRequired()) {
96 // Collect required contexts that exist but are missing a value.
97 $missing_value[] = $plugin_context_id;
100 elseif ($plugin_context_definition->isRequired()) {
101 // Collect required contexts that are missing.
102 $missing_value[] = $plugin_context_id;
105 // Ignore mappings for optional missing context.
106 unset($mappings[$plugin_context_id]);
110 // If there are any required contexts without a value, throw an exception.
111 if ($missing_value) {
112 throw new ContextException(sprintf('Required contexts without a value: %s.', implode(', ', $missing_value)));
115 // If there are any mappings that were not satisfied, throw an exception.
116 if (!empty($mappings)) {
117 throw new ContextException('Assigned contexts were not satisfied: ' . implode(',', array_keys($mappings)));