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..f172086 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -0,0 +1,83 @@ +contextDefinition = $contextDefinition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContext(). + */ + public function setContext($context) { + $context = $this->validate($context); + $this->context = $context; + } + + /** + * Implements ContextInterface::getContext(). + */ + public function getContext() { + return $this->context; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextDefinition(). + */ + public function setContextDefinition($contextDefinition) { + $this->contextDefinition = $contextDefinition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextDefinition(). + */ + public function getContextDefinition() { + return $this->contextDefinition; + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::validate(). + */ + public function validate($context) { + // Check to make sure we have a class name, and that the passed context is + // an instance of that class name. + 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."); + } + +} 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..2eb5348 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php @@ -0,0 +1,64 @@ +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() { + $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 \Drupal\Component\Plugin\ContextAwarePluginInterface::getContext(). + */ + public function getContext($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 \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). + */ + public function getContextValues() { + $contexts = array(); + foreach ($this->getContexts() as $key => $context) { + $contexts[$key] = $context->getContext(); + } + return $contexts; + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValue(). + */ + public function getContextValue($key) { + return $this->getContext($key)->getContext(); + } + + /** + * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::setContextValue(). + */ + public function setContextValue($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/ContextAwarePluginInterface.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php new file mode 100644 index 0000000..e97e874 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php @@ -0,0 +1,90 @@ +contextDefinition)) { + $typed_data_manager = new TypedDataManager(); + $typed_data = $typed_data_manager->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."); + } + return parent::validate($context); + } + +} diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php new file mode 100644 index 0000000..f8254c2 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.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/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php new file mode 100644 index 0000000..0236fed --- /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 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 to get a valid context wrapper 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 wrapper. + 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 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 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. + 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 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' => 'Drupal\user\Plugin\Core\Entity\User')); + + // Test the getContextDefinition() method for a valid context. + $this->assertEqual($plugin->getContextDefinition('user'), '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 TypedData Context Plugins + $typed_data_plugin = $manager->createInstance('string_context'); + + // Try to get a valid context 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 context 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 appropriately and check the title. + $typed_data_plugin->setContextValue('string', $name); + $this->assertEqual($name, $typed_data_plugin->getTitle()); + + // Test Complex compound contexts. + $complex_plugin = $manager->createInstance('complex_context'); + + // With no contexts set, get the context wrappers. + 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, get the contexts. + 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. + $complex_plugin->setContextValue('user', $user); + + // With only the user context set, get the context wrappers. + 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, get the contexts. + 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']->getContext()->label(), $user->label()); + $this->assertEqual($context_wrappers['node']->getContext()->label(), $node->label()); + + // Make sure what comes out of Contexts 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 5db9322..32553d5 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 @@ 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' + ), + ), + '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' => 'Drupal\user\Plugin\Core\Entity\User', + 'node' => '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 fcf1824..042bd22 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 @@ 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' + ), + )); + + // 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' => 'Drupal\user\Plugin\Core\Entity\User', + 'node' => '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 0000000..1cb657a --- /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 0000000..3fc1f84 --- /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 0000000..46cddaf --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/TypedDataStringBlock.php @@ -0,0 +1,24 @@ +getContextValue('string'); + return $context->getValue(); + } +}