diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php new file mode 100644 index 0000000..b377c75 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -0,0 +1,55 @@ +contextDefinition = $contextDefinition; + } + + public function setContext($context) { + $context = $this->validate($context); + $this->context = $context; + } + + public function getContext() { + return $this->context; + } + + public function validate($context) { + if (is_string($this->contextDefinition)) { + if ($context instanceof $this->contextDefinition) { + return $context; + } + throw new ContextException("The context passed was not an instance of $this->contextDefinition."); + } + throw new ContextException("An error was encountered while trying to validate the context."); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php b/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php new file mode 100644 index 0000000..a48d3e6 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php @@ -0,0 +1,97 @@ +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 set values for all defined contexts. + * + * @return array + * Returns the array of all set contexts. + */ + public function getContexts() { + if (empty($this->configuration['context'])) { + throw new PluginException("There are no set contexts."); + } + return $this->configuration['context']; + } + + /** + * 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(); + } + + /** + * 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]; + } + + /** + * 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/Exception/ContextException.php b/core/lib/Drupal/Component/Plugin/Exception/ContextException.php new file mode 100644 index 0000000..fbb1c2d --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Exception/ContextException.php @@ -0,0 +1,15 @@ +contextDefinition)) { + if ($context instanceof $this->contextDefinition) { + return $context; + } + throw new ContextException("The context passed was not an instance of $this->contextDefinition."); + } + // Check to see if we have a typed data definition instead of a class name. + if (is_array($this->contextDefinition)) { + $typed_data = typed_data()->create($this->contextDefinition, $context); + // If we do have a typed data definition, validate it and return the + // typed data instance instead. + if ($typed_data->validate()) { + return $typed_data; + } + throw new ContextException("The context passed could not be validated through typed data."); + } + throw new ContextException("An error was encountered while trying to validate the context."); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php b/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php new file mode 100644 index 0000000..ea47c33 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php @@ -0,0 +1,32 @@ +getContextDefinition($key); + $this->configuration['context'][$key] = new Context($class); + $this->configuration['context'][$key]->setContext($value); + + return $this; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php new file mode 100644 index 0000000..b08aadb --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php @@ -0,0 +1,48 @@ + 'Contextual Plugins', + 'description' => 'Tests that contexts are properly set and working within plugins.', + 'group' => 'Plugin API', + ); + } + + /** + * Tests getDefinitions() and getDefinition() with a derivativeDecorator. + */ + function testContext() { + $name = $this->randomName(); + $manager = new MockBlockManager(); + $plugin = $manager->createInstance('user_name'); + // Create a node, add it as context, catch the exception. + $node = entity_create('node', array('title' => $name)); + try { + $plugin->setContext('user', $node); + } + catch (ContextException $e) { + $this->assertEqual($e->getMessage(), 'The context passed was not an instance of Drupal\user\Plugin\Core\Entity\User.'); + } + $user = entity_create('user', array('name' => $name)); + $plugin->setContext('user', $user); + $this->assertEqual($user->label(), $plugin->getTitle()); + } +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php index 5db9322..b62878e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php @@ -70,6 +70,13 @@ public function setUp() { 'label' => 'Layout Foo', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock', ), + 'user_name' => array( + 'label' => 'User Name', + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', + 'context' => array( + 'user' => 'Drupal\user\Plugin\Core\Entity\User' + ), + ), ); $this->defaultsTestPluginExpectedDefinitions = array( 'test_block1' => array( diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php index fcf1824..7f65b0c 100644 --- a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php @@ -66,6 +66,16 @@ public function __construct() { 'derivative' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlockDeriver', )); + // A block plugin that requires context to function. This block requires a + // user object in order to return the user name from the getTitle() method. + $this->discovery->setDefinition('user_name', array( + 'label' => t('User Name'), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', + 'context' => array( + 'user' => 'Drupal\user\Plugin\Core\Entity\User' + ), + )); + // In addition to finding all of the plugins available for a type, a plugin // type must also be able to create instances of that plugin. For example, a // specific instance of a "Main menu" menu block, configured to show just 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 new file mode 100644 index 0000000..ee333d8 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php @@ -0,0 +1,24 @@ +getContext('user'); + return $user->label(); + } +}