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..f6b9747 --- /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($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 variable 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 (is_string($this->contextDefinition)) { + if ($value instanceof $this->contextDefinition) { + return $value; + } + 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..ab88543 --- /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 0000000..68d8012 --- /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) { + // Check to see if we have a typed data definition instead of a class name. + if (is_array($this->contextDefinition)) { + $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 0000000..028befd --- /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 0000000..2eab5fa --- /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 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' => '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 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 5db9322..f91675c 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/system.module b/core/modules/system/system.module index 322e416..0fe64f8 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1177,6 +1177,41 @@ function system_plugin_ui_access($plugin, $facet = NULL) { return $plugin_ui->access($facet); } + +/** + * Provides a generic wrapper for multi page entity wizard forms. + * + * @param $entity_type + * The type of entity we are working with. This will be used for + * entity_create() calls as well as unique storage within tempstore. + * @param $step + * The current step in the form wizard process. + * @param $id + * The unique identifier of the entity the form wizard is building. + * + * @return array + * The renderable array from the current step of the entity's available form + * controllers. + */ +function system_wizard_form($entity_type, $step, $id = NULL) { + if (empty($id)) { + $entity = entity_create($entity_type, array()); + } + else { + $entity = drupal_container()->get('user.tempstore')->get($entity_type)->get($id); + if (!method_exists($entity, 'entityType')) { + $entity = entity_load($entity_type, $id); + } + } + $definition = $entity->entityInfo(); + // This could be any entity form controller and is not necessarily documented + // in the steps. If it is not in the steps, it will not have a title. + if (isset($definition['steps'][$step])) { + drupal_set_title($definition['steps'][$step]); + } + return entity_get_form($entity, $step); +} + /** * Implements hook_forms(). */ 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..4ef8283 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..14e0ab3 --- /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'); + } +}