diff --git a/core/lib/Drupal/Core/Plugin/Context/Context.php b/core/lib/Drupal/Core/Plugin/Context/Context.php index bc9f72c..cf919d5 100644 --- a/core/lib/Drupal/Core/Plugin/Context/Context.php +++ b/core/lib/Drupal/Core/Plugin/Context/Context.php @@ -9,6 +9,7 @@ use Drupal\Component\Plugin\Context\Context as ComponentContext; use Drupal\Component\Plugin\Exception\ContextException; +use Drupal\Core\TypedData\TypedDataManager; /** * A Drupal specific context wrapper class. @@ -33,7 +34,8 @@ public function validate($context) { } // 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); + $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()) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php index 19a53f9..470b795 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php @@ -7,7 +7,7 @@ namespace Drupal\system\Tests\Plugin; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; use Drupal\plugin_test\Plugin\MockBlockManager; use Drupal\plugin_test\MockNode; use Drupal\plugin_test\MockUser; @@ -17,7 +17,9 @@ /** * Tests that derivative plugins are correctly discovered. */ -class ContextPluginTest extends UnitTestBase { +class ContextPluginTest extends DrupalUnitTestBase { + + public static $modules = array('system'); public static function getInfo() { return array( @@ -90,5 +92,28 @@ function testContext() { $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->getContext('string'); + } + 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(); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The string context is not yet set.'); + } + + // Set the context appropriately. + $typed_data_plugin->setContext('string', $name); + $this->assertEqual($name, $typed_data_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 f80daf8..892d30c0 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php @@ -77,6 +77,13 @@ public function setUp() { 'user' => 'Drupal\plugin_test\MockUser' ), ), + 'string_context' => array( + 'label' => 'String Typed Data', + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\TypedDataStringBlock', + 'context' => array( + 'string' => array('type' => 'string'), + ), + ) ); $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 2ba5dd9..793c012 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 @@ -76,6 +76,15 @@ public function __construct() { ), )); + // 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'), + ), + )); + // 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 index ee333d8..86b4365 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\Component\Plugin\ContextualPluginBase; +use Drupal\Core\Plugin\ContextualPluginBase; use Drupal\Component\Plugin\Discovery\DiscoveryInterface; /** 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..2c62f49 --- /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 @@ +getContext('string'); + return $context->getValue(); + } +}