summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal
diff options
context:
space:
mode:
authorLee Rowlands2018-06-25 03:37:08 (GMT)
committerLee Rowlands2018-06-25 03:37:08 (GMT)
commit1b66e5369fc70c4a755a852264e08df35ec4bda5 (patch)
tree02683f3dae1fed005471d636850a763afa6c1695 /core/lib/Drupal
parent41c3a88e35dcc1c5c628769264922aa1e13d98be (diff)
Issue #2961822 by phenaproxima, tim.plunkett: Support object-based plugin definitions in ContextHandlerHEAD8.6.x
Diffstat (limited to 'core/lib/Drupal')
-rw-r--r--core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php71
-rw-r--r--core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionTrait.php60
-rw-r--r--core/lib/Drupal/Core/Plugin/Context/ContextHandler.php36
3 files changed, 159 insertions, 8 deletions
diff --git a/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php
new file mode 100644
index 0000000..d1ff953
--- /dev/null
+++ b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Drupal\Component\Plugin\Definition;
+
+use Drupal\Component\Plugin\Context\ContextDefinitionInterface;
+
+/**
+ * Provides an interface for plugin definitions which use contexts.
+ *
+ * @ingroup Plugin
+ */
+interface ContextAwarePluginDefinitionInterface extends PluginDefinitionInterface {
+
+ /**
+ * Checks if the plugin defines a particular context.
+ *
+ * @param string $name
+ * The context name.
+ *
+ * @return bool
+ * TRUE if the plugin defines the given context, otherwise FALSE.
+ */
+ public function hasContextDefinition($name);
+
+ /**
+ * Returns all context definitions for this plugin.
+ *
+ * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
+ * The context definitions.
+ */
+ public function getContextDefinitions();
+
+ /**
+ * Returns a particular context definition for this plugin.
+ *
+ * @param string $name
+ * The context name.
+ *
+ * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
+ * The context definition.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\ContextException
+ * Thrown if the plugin does not define the given context.
+ */
+ public function getContextDefinition($name);
+
+ /**
+ * Adds a context to this plugin definition.
+ *
+ * @param string $name
+ * The context name.
+ * @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $definition
+ * The context definition.
+ *
+ * @return $this
+ * The called object.
+ */
+ public function addContextDefinition($name, ContextDefinitionInterface $definition);
+
+ /**
+ * Removes a context definition from this plugin.
+ *
+ * @param string $name
+ * The context name.
+ *
+ * @return $this
+ * The called object.
+ */
+ public function removeContextDefinition($name);
+
+}
diff --git a/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionTrait.php b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionTrait.php
new file mode 100644
index 0000000..54b8e5f
--- /dev/null
+++ b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionTrait.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Drupal\Component\Plugin\Definition;
+
+use Drupal\Component\Plugin\Context\ContextDefinitionInterface;
+use Drupal\Component\Plugin\Exception\ContextException;
+
+/**
+ * Provides a trait for context-aware object-based plugin definitions.
+ */
+trait ContextAwarePluginDefinitionTrait {
+
+ /**
+ * The context definitions for this plugin definition.
+ *
+ * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
+ */
+ protected $contextDefinitions = [];
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::hasContextDefinition().
+ */
+ public function hasContextDefinition($name) {
+ return array_key_exists($name, $this->contextDefinitions);
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions().
+ */
+ public function getContextDefinitions() {
+ return $this->contextDefinitions;
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition().
+ */
+ public function getContextDefinition($name) {
+ if ($this->hasContextDefinition($name)) {
+ return $this->contextDefinitions[$name];
+ }
+ throw new ContextException($this->id() . " does not define a '$name' context");
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition().
+ */
+ public function addContextDefinition($name, ContextDefinitionInterface $definition) {
+ $this->contextDefinitions[$name] = $definition;
+ return $this;
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition().
+ */
+ public function removeContextDefinition($name) {
+ unset($this->contextDefinitions[$name]);
+ return $this;
+ }
+
+}
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
index f174d46..c7517a9 100644
--- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
@@ -2,6 +2,7 @@
namespace Drupal\Core\Plugin\Context;
+use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\Exception\MissingValueContextException;
use Drupal\Core\Cache\CacheableDependencyInterface;
@@ -17,19 +18,38 @@ class ContextHandler implements ContextHandlerInterface {
*/
public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
return array_filter($definitions, function ($plugin_definition) use ($contexts) {
- // If this plugin doesn't need any context, it is available to use.
- // @todo Support object-based plugin definitions in
- // https://www.drupal.org/project/drupal/issues/2961822.
- if (!is_array($plugin_definition) || !isset($plugin_definition['context'])) {
- return TRUE;
- }
+ $context_definitions = $this->getContextDefinitions($plugin_definition);
- // Check the set of contexts against the requirements.
- return $this->checkRequirements($contexts, $plugin_definition['context']);
+ if ($context_definitions) {
+ // Check the set of contexts against the requirements.
+ return $this->checkRequirements($contexts, $context_definitions);
+ }
+ // If this plugin doesn't need any context, it is available to use.
+ return TRUE;
});
}
/**
+ * Returns the context definitions associated with a plugin definition.
+ *
+ * @param array|\Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface $plugin_definition
+ * The plugin definition.
+ *
+ * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]|null
+ * The context definitions, or NULL if the plugin definition does not
+ * support contexts.
+ */
+ protected function getContextDefinitions($plugin_definition) {
+ if ($plugin_definition instanceof ContextAwarePluginDefinitionInterface) {
+ return $plugin_definition->getContextDefinitions();
+ }
+ if (is_array($plugin_definition) && isset($plugin_definition['context'])) {
+ return $plugin_definition['context'];
+ }
+ return NULL;
+ }
+
+ /**
* {@inheritdoc}
*/
public function checkRequirements(array $contexts, array $requirements) {