diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php index ce8ccf4..48920d7 100644 --- a/core/lib/Drupal/Component/Plugin/Context/Context.php +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -29,19 +29,46 @@ class Context { */ protected $contextDefinition; + /** + * Sets the contextDefinition for us without needing to make a call to + * $this->setContextDefinition(). + */ public function __construct($contextDefinition) { $this->contextDefinition = $contextDefinition; } + /** + * Implements ContextInterface::setContext(). + */ public function setContext($context) { $context = $this->validate($context); $this->context = $context; } + /** + * Implements ContextInterface::getContext(). + */ public function getContext() { return $this->context; } + /** + * Implements ContextInterface::setContextDefinition(). + */ + public function setContextDefinition($contextDefinition) { + $this->contextDefinition = $contextDefinition; + } + + /** + * Implements ContextInterface::getContextDefinition(). + */ + public function getContextDefinition() { + return $this->contextDefinition; + } + + /** + * Implements ContextInterface::validate(). + */ public function validate($context) { if (is_string($this->contextDefinition)) { if ($context instanceof $this->contextDefinition) { diff --git a/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php new file mode 100644 index 0000000..46ef019 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php @@ -0,0 +1,63 @@ +getDefinition(); - return !empty($definition['context']) ? $definition['context'] : NULL; - } - - /** - * Returns the a specific context class definition of the plugin. - * - * @return string - * The name of a class to which the set context must conform. - */ - public function getContextDefinition($key) { - $definition = $this->getDefinition(); - if (empty($definition['context'][$key])) { - throw new PluginException("The $key context is not a valid context."); - } - return $definition['context'][$key]; - } - - /** - * Returns the wrappers for set values for all defined contexts. - * - * @return array - * Returns the array of all set context wrappers. - */ - public function getContextsWrappers() { - $definition = $this->getDefinition(); - // If there are no contexts defined by the plugin, return an empty array. - if (empty($definition['context'])) { - return array(); - } - if (empty($this->configuration['context'])) { - throw new PluginException("There are no set contexts."); - } - $contexts = array(); - foreach (array_keys($definition['context']) as $key) { - if (empty($this->configuration['context'][$key])) { - throw new PluginException("The $key context is not yet set."); - } - $contexts[$key] = $this->configuration['context'][$key]; - } - return $contexts; - } - - /** - * Returns the set context wrapper for a defined context. - * - * @return object - * Returns a context wrapper object. - */ - public function getContextWrapper($key) { - $definition = $this->getDefinition(); - if (empty($definition['context'][$key])) { - throw new PluginException("The $key context is not a valid context."); - } - if (empty($this->configuration['context'][$key])) { - throw new PluginException("The $key context is not yet set."); - } - - return $this->configuration['context'][$key]; - } - - /** - * Returns the set values for all defined contexts. - * - * @return array - * Returns the array of all set contexts. - */ - public function getContexts() { - $contexts = array(); - foreach ($this->getContextsWrappers() as $key => $context) { - $contexts[$key] = $context->getContext(); - } - return $contexts; - } - - /** - * Returns the set value for a defined context. - * - * @return object - * Returns instantiated object of a context. - */ - public function getContext($key) { - return $this->getContextWrapper($key)->getContext(); - } - - /** - * Sets a defined context to the passed value. - * - * @return $this - * Returns the plugin instance. - */ - public function setContext($key, $value) { - $class = $this->getContextDefinition($key); - $this->configuration['context'][$key] = new Context($class); - $this->configuration['context'][$key]->setContext($value); - - return $this; - } - -} diff --git a/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginBase.php b/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginBase.php new file mode 100644 index 0000000..c077432 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginBase.php @@ -0,0 +1,102 @@ +getDefinition(); + return !empty($definition['context']) ? $definition['context'] : NULL; + } + + /** + * Implements ContextuallyAwarePluginInterface::getContextDefinition(). + */ + public function getContextDefinition($key) { + $definition = $this->getDefinition(); + if (empty($definition['context'][$key])) { + throw new PluginException("The $key context is not a valid context."); + } + return $definition['context'][$key]; + } + + /** + * Implements ContextuallyAwarePluginInterface::getContextWrappers(). + */ + public function getContextsWrappers() { + $definition = $this->getDefinition(); + // If there are no contexts defined by the plugin, return an empty array. + if (empty($definition['context'])) { + return array(); + } + if (empty($this->configuration['context'])) { + throw new PluginException("There are no set contexts."); + } + $contexts = array(); + foreach (array_keys($definition['context']) as $key) { + if (empty($this->configuration['context'][$key])) { + throw new PluginException("The $key context is not yet set."); + } + $contexts[$key] = $this->configuration['context'][$key]; + } + return $contexts; + } + + /** + * Implements ContextuallyAwarePluginInterface::getContextWrapper(). + */ + public function getContextWrapper($key) { + $definition = $this->getDefinition(); + if (empty($definition['context'][$key])) { + throw new PluginException("The $key context is not a valid context."); + } + if (empty($this->configuration['context'][$key])) { + throw new PluginException("The $key context is not yet set."); + } + + return $this->configuration['context'][$key]; + } + + /** + * Implements ContextuallyAwarePluginInterface::getContexts(). + */ + public function getContexts() { + $contexts = array(); + foreach ($this->getContextsWrappers() as $key => $context) { + $contexts[$key] = $context->getContext(); + } + return $contexts; + } + + /** + * Implements ContextuallyAwarePluginInterface::getContext(). + */ + public function getContext($key) { + return $this->getContextWrapper($key)->getContext(); + } + + /** + * Implements ContextuallyAwarePluginInterface::setContext(). + */ + public function setContext($key, $value) { + $context_definition = $this->getContextDefinition($key); + $this->configuration['context'][$key] = new Context($context_definition); + $this->configuration['context'][$key]->setContext($value); + + return $this; + } + +} diff --git a/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginInterface.php b/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginInterface.php new file mode 100644 index 0000000..35f9bf8 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextuallyAwarePluginInterface.php @@ -0,0 +1,91 @@ +getContextDefinition($key); - $this->configuration['context'][$key] = new Context($class); - $this->configuration['context'][$key]->setContext($value); - - return $this; - } - -} diff --git a/core/lib/Drupal/Core/Plugin/ContextuallyAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextuallyAwarePluginBase.php new file mode 100644 index 0000000..1a0805e --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContextuallyAwarePluginBase.php @@ -0,0 +1,33 @@ +getContextDefinition($key); + $this->configuration['context'][$key] = new Context($context_definition); + $this->configuration['context'][$key]->setContext($value); + + return $this; + } + +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php index fe25bc2..8c1e0f4 100644 --- a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php @@ -7,14 +7,14 @@ namespace Drupal\plugin_test\Plugin\plugin_test\mock_block; -use Drupal\Core\Plugin\ContextualPluginBase; +use Drupal\Core\Plugin\ContextuallyAwarePluginBase; /** * Implementation of a complex context plugin used by Plugin API context tests. * * @see Drupal\plugin_test\Plugin\MockBlockManager */ -class MockComplexContextBlock extends ContextualPluginBase { +class MockComplexContextBlock extends ContextuallyAwarePluginBase { public function getTitle() { $user = $this->getContext('user'); diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php index 86b4365..73224de 100644 --- a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php @@ -7,7 +7,7 @@ namespace Drupal\plugin_test\Plugin\plugin_test\mock_block; -use Drupal\Core\Plugin\ContextualPluginBase; +use Drupal\Core\Plugin\ContextuallyAwarePluginBase; use Drupal\Component\Plugin\Discovery\DiscoveryInterface; /** @@ -15,7 +15,7 @@ * * @see Drupal\plugin_test\Plugin\MockBlockManager */ -class MockUserNameBlock extends ContextualPluginBase { +class MockUserNameBlock extends ContextuallyAwarePluginBase { public function getTitle() { $user = $this->getContext('user'); diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php index 2c62f49..ef66767 100644 --- a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php @@ -7,7 +7,7 @@ namespace Drupal\plugin_test\Plugin\plugin_test\mock_block; -use Drupal\Core\Plugin\ContextualPluginBase; +use Drupal\Core\Plugin\ContextuallyAwarePluginBase; /** * Implementation of a String TypedData contextual block plugin used by Plugin @@ -15,7 +15,7 @@ * * @see Drupal\plugin_test\Plugin\MockBlockManager */ -class TypedDataStringBlock extends ContextualPluginBase { +class TypedDataStringBlock extends ContextuallyAwarePluginBase { public function getTitle() { $context = $this->getContext('string');