From 71364b1a81ca6749f66af48c82980dcd6d152d6e Mon Sep 17 00:00:00 2001 From: Dries Date: Tue, 12 Feb 2013 15:51:59 -0500 Subject: [PATCH] Issue #1896076 by EclipseGc, fago: Added Contextual Plugins and supporting code. --- .../Component/Plugin/Context/Context.php | 86 ++++++++ .../Plugin/Context/ContextInterface.php | 68 ++++++ .../Plugin/ContextAwarePluginBase.php | 109 ++++++++++ .../Plugin/ContextAwarePluginInterface.php | 91 ++++++++ .../Plugin/Exception/ContextException.php | 15 ++ .../Drupal/Core/Plugin/Context/Context.php | 74 +++++++ .../Core/Plugin/ContextAwarePluginBase.php | 33 +++ .../system/Tests/Plugin/ContextPluginTest.php | 203 ++++++++++++++++++ .../system/Tests/Plugin/PluginTestBase.php | 22 ++ .../plugin_test/Plugin/MockBlockManager.php | 29 +++ .../mock_block/MockComplexContextBlock.php | 24 +++ .../mock_block/MockUserNameBlock.php | 24 +++ .../mock_block/TypedDataStringBlock.php | 23 ++ 13 files changed, 801 insertions(+) create mode 100644 core/lib/Drupal/Component/Plugin/Context/Context.php create mode 100644 core/lib/Drupal/Component/Plugin/Context/ContextInterface.php create mode 100644 core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php create mode 100644 core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php create mode 100644 core/lib/Drupal/Component/Plugin/Exception/ContextException.php create mode 100644 core/lib/Drupal/Core/Plugin/Context/Context.php create mode 100644 core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php create mode 100644 core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php create mode 100644 core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php create mode 100644 core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockUserNameBlock.php create mode 100644 core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php 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 000000000000..9f4396b62371 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -0,0 +1,86 @@ +contextDefinition = $context_definition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextValue(). + */ + public function setContextValue($value) { + $value = $this->validate($value); + $this->contextValue = $value; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextValue(). + */ + public function getContextValue() { + return $this->contextValue; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextDefinition(). + */ + public function setContextDefinition(array $context_definition) { + $this->contextDefinition = $context_definition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextDefinition(). + */ + public function getContextDefinition() { + return $this->contextDefinition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::validate(). + * + * The default validation method only supports instance of checks between the + * contextDefintion and the contextValue. Other formats of context + * definitions can be supported through a subclass. + */ + public function validate($value) { + // Check to make sure we have a class name, and that the passed context is + // an instance of that class name. + if (!empty($this->contextDefinition['class'])) { + if ($value instanceof $this->contextDefinition['class']) { + return $value; + } + throw new ContextException("The context passed was not an instance of {$this->contextDefinition['class']}."); + } + throw new ContextException("An error was encountered while trying to validate the context."); + } + +} 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 000000000000..5c4373dff024 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php @@ -0,0 +1,68 @@ +getDefinition(); + return !empty($definition['context']) ? $definition['context'] : NULL; + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::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 \Drupal\Component\Plugin\ContextAwarePluginInterface::getContexts(). + */ + public function getContexts() { + $definitions = $this->getContextDefinitions(); + // If there are no contexts defined by the plugin, return an empty array. + if (empty($definitions)) { + return array(); + } + if (empty($this->context)) { + throw new PluginException("There are no set contexts."); + } + $contexts = array(); + foreach (array_keys($definitions) as $key) { + if (empty($this->context[$key])) { + throw new PluginException("The $key context is not yet set."); + } + $contexts[$key] = $this->context[$key]; + } + return $contexts; + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContext(). + */ + public function getContext($key) { + // Check for a valid context definition. + $this->getContextDefinition($key); + // Check for a valid context value. + if (empty($this->context[$key])) { + throw new PluginException("The $key context is not yet set."); + } + + return $this->context[$key]; + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). + */ + public function getContextValues() { + $contexts = array(); + foreach ($this->getContexts() as $key => $context) { + $contexts[$key] = $context->getContextValue(); + } + return $contexts; + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValue(). + */ + public function getContextValue($key) { + return $this->getContext($key)->getContextValue(); + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::setContextValue(). + */ + public function setContextValue($key, $value) { + $context_definition = $this->getContextDefinition($key); + $this->context[$key] = new Context($context_definition); + $this->context[$key]->setContextValue($value); + + return $this; + } + +} diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php new file mode 100644 index 000000000000..68d8012e319b --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php @@ -0,0 +1,91 @@ +getDefinition($typed_value->getType()); + if (!empty($type_definition['primitive type'])) { + return $typed_value->getValue(); + } + } + return $typed_value; + } + + /** + * Gets the context value as typed data object. + * + * parent::getContextValue() does not do all the processing required to + * return plain value of a TypedData object. This class overrides that method + * to return the appropriate values from TypedData objects, but the object + * itself can be useful as well, so this method is provided to allow for + * access to the TypedData object. Since parent::getContextValue() already + * does all the processing we need, we simply proxy to it here. + * + * @return \Drupal\Core\TypedData\TypedDataInterface + */ + public function getTypedContext() { + return parent::getContextValue(); + } + + /** + * Override for \Drupal\Component\Plugin\Context\Context::validate(). + */ + public function validate($value) { + if (!empty($this->contextDefinition['type'])) { + $typed_data_manager = new TypedDataManager(); + $typed_data = $typed_data_manager->create($this->contextDefinition, $value); + // If we do have a typed data definition, validate it and return the + // typed data instance instead. + $violations = $typed_data->validate(); + if (count($violations) == 0) { + return $typed_data; + } + throw new ContextException("The context passed could not be validated through typed data."); + } + return parent::validate($value); + } + +} diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php new file mode 100644 index 000000000000..028befde28b6 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @@ -0,0 +1,33 @@ +getContextDefinition($key); + $this->context[$key] = new Context($context_definition); + $this->context[$key]->setContextValue($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 000000000000..888f1e66149a --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php @@ -0,0 +1,203 @@ + 'Contextual Plugins', + 'description' => 'Tests that contexts are properly set and working within plugins.', + 'group' => 'Plugin API', + ); + } + + protected function setUp() { + parent::setUp(); + $this->installSchema('node', 'node_type'); + } + + /** + * Tests basic context definition and value getters and setters. + */ + 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 to get a valid context that has not been set. + try { + $plugin->getContext('user'); + $this->fail('The user context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The user context is not yet set.'); + } + + // Try to get an invalid context. + try { + $plugin->getContext('node'); + $this->fail('The node context should not be a valid context.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not a valid context.'); + } + + // Try to get a valid context value that has not been set. + try { + $plugin->getContextValue('user'); + $this->fail('The user context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The user context is not yet set.'); + } + + // Try to call a method of the plugin that requires context before it has + // been set. + try { + $plugin->getTitle(); + $this->fail('The user context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The user context is not yet set.'); + } + + // Try to get a context value that is not valid. + try { + $plugin->getContextValue('node'); + $this->fail('The node context should not be a valid context.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not a valid context.'); + } + + // Try to pass the wrong class type as a context value. + try { + $plugin->setContextValue('user', $node); + $this->fail('The node context should fail validation for a user context.'); + } + catch (ContextException $e) { + $this->assertEqual($e->getMessage(), 'The context passed was not an instance of Drupal\user\Plugin\Core\Entity\User.'); + } + + // Set an appropriate context value appropriately and check to make sure + // its methods work as expected. + $user = entity_create('user', array('name' => $name)); + $plugin->setContextValue('user', $user); + $this->assertEqual($user->label(), $plugin->getTitle()); + + // Test the getContextDefinitions() method. + $this->assertIdentical($plugin->getContextDefinitions(), array('user' => array('class' => 'Drupal\user\Plugin\Core\Entity\User'))); + + // Test the getContextDefinition() method for a valid context. + $this->assertEqual($plugin->getContextDefinition('user'), array('class' => 'Drupal\user\Plugin\Core\Entity\User')); + + // Test the getContextDefinition() method for an invalid context. + try { + $plugin->getContextDefinition('node'); + $this->fail('The node context should not be a valid context.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not a valid context.'); + } + + // Test typed data context plugins. + $typed_data_plugin = $manager->createInstance('string_context'); + + // Try to get a valid context value that has not been set. + try { + $typed_data_plugin->getContextValue('string'); + $this->fail('The string context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The string context is not yet set.'); + } + + // Try to call a method of the plugin that requires a context value before + // it has been set. + try { + $typed_data_plugin->getTitle(); + $this->fail('The string context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The string context is not yet set.'); + } + + // Set the context value appropriately and check the title. + $typed_data_plugin->setContextValue('string', $name); + $this->assertEqual($name, $typed_data_plugin->getTitle()); + + // Test Complex compound context handling. + $complex_plugin = $manager->createInstance('complex_context'); + + // With no contexts set, try to get the contexts. + try { + $complex_plugin->getContexts(); + $this->fail('There should not be any contexts set yet.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'There are no set contexts.'); + } + + // With no contexts set, try to get the context values. + try { + $complex_plugin->getContextValues(); + $this->fail('There should not be any contexts set yet.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'There are no set contexts.'); + } + + // Set the user context value. + $complex_plugin->setContextValue('user', $user); + + // With only the user context set, try to get the contexts. + try { + $complex_plugin->getContexts(); + $this->fail('The node context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not yet set.'); + } + + // With only the user context set, try to get the context values. + try { + $complex_plugin->getContextValues(); + $this->fail('The node context should not yet be set.'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not yet set.'); + } + + $complex_plugin->setContextValue('node', $node); + $context_wrappers = $complex_plugin->getContexts(); + // Make sure what came out of the wrappers is good. + $this->assertEqual($context_wrappers['user']->getContextValue()->label(), $user->label()); + $this->assertEqual($context_wrappers['node']->getContextValue()->label(), $node->label()); + + // Make sure what comes out of the context values is good. + $contexts = $complex_plugin->getContextValues(); + $this->assertEqual($contexts['user']->label(), $user->label()); + $this->assertEqual($contexts['node']->label(), $node->label()); + + // Test the title method for the complex context plugin. + $this->assertEqual($user->label() . ' -- ' . $node->label(), $complex_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 5db932203ca0..c927f7eb2419 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,28 @@ abstract class PluginTestBase extends UnitTestBase { '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' => array('class' => 'Drupal\user\Plugin\Core\Entity\User') + ), + ), + 'string_context' => array( + 'label' => 'String typed data', + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock', + 'context' => array( + 'string' => array('type' => 'string'), + ), + ), + 'complex_context' => array( + 'label' => 'Complex context', + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock', + 'context' => array( + 'user' => array('class' => 'Drupal\user\Plugin\Core\Entity\User'), + 'node' => array('class' => 'Drupal\node\Plugin\Core\Entity\Node'), + ), + ), ); $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 fcf18245125f..7e292597367a 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,35 @@ class MockBlockManager extends PluginManagerBase { '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' => array('class' => 'Drupal\user\Plugin\Core\Entity\User') + ), + )); + + // A block plugin that requires a typed data string context to function. + $this->discovery->setDefinition('string_context', array( + 'label' => t('String typed data'), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock', + 'context' => array( + 'string' => array('type' => 'string'), + ), + )); + + // A complex context plugin that requires both a user and node for context. + $this->discovery->setDefinition('complex_context', array( + 'label' => t('Complex context'), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock', + 'context' => array( + 'user' => array('class' => 'Drupal\user\Plugin\Core\Entity\User'), + 'node' => array('class' => 'Drupal\node\Plugin\Core\Entity\Node'), + ), + )); + // 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/MockComplexContextBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php new file mode 100644 index 000000000000..1cb657a4a61d --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockComplexContextBlock.php @@ -0,0 +1,24 @@ +getContextValue('user'); + $node = $this->getContextValue('node'); + return $user->label() . ' -- ' . $node->label(); + } +} 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 000000000000..3fc1f849d1e9 --- /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 @@ +getContextValue('user'); + return $user->label(); + } +} 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 new file mode 100644 index 000000000000..14e0ab3bf7ef --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php @@ -0,0 +1,23 @@ +getContextValue('string'); + } +}