diff --git a/core/lib/Drupal/Component/Plugin/Discovery/DefaultsDecorator.php b/core/lib/Drupal/Component/Plugin/Discovery/DefaultsDecorator.php index 401031a..3a1904c 100644 --- a/core/lib/Drupal/Component/Plugin/Discovery/DefaultsDecorator.php +++ b/core/lib/Drupal/Component/Plugin/Discovery/DefaultsDecorator.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Component\Plugin\Discovery\DefaultsDecorator. + * Contains Drupal\Component\Plugin\Discovery\DefaultsDecorator. */ namespace Drupal\Component\Plugin\Discovery; @@ -56,8 +56,8 @@ public function getDefinition($plugin_id) { */ public function getDefinitions() { $definitions = $this->decorated->getDefinitions(); - foreach ($definitions as $plugin_id => &$definition) { - $definition = NestedArray::mergeDeep($this->defaults, $definition); + foreach ($definitions as $plugin_id => $definition) { + $definitions[$plugin_id] = NestedArray::mergeDeep($this->defaults, $definition); } return $definitions; } @@ -68,4 +68,5 @@ public function getDefinitions() { public function __call($method, $args) { return call_user_func_array(array($this->decorated, $method), $args); } + } diff --git a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php index b80a9e9..ee6e72c 100644 --- a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php +++ b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php @@ -37,14 +37,23 @@ * Implements Drupal\Component\Plugin\PluginManagerInterface::getDefinition(). */ public function getDefinition($plugin_id) { - return $this->discovery->getDefinition($plugin_id); + $definition = $this->discovery->getDefinition($plugin_id); + if (isset($definition)) { + $this->processDefinition($definition, $plugin_id); + } + return $definition; } /** * Implements Drupal\Component\Plugin\PluginManagerInterface::getDefinitions(). */ public function getDefinitions() { - return $this->discovery->getDefinitions(); + $definitions = $this->discovery->getDefinitions(); + foreach ($definitions as $plugin_id => &$definition) { + $this->processDefinition($definition, $plugin_id); + } + + return $definitions; } /** @@ -60,4 +69,15 @@ public function createInstance($plugin_id, array $configuration = array()) { public function getInstance(array $options) { return $this->mapper->getInstance($options); } + + /** + * Performs extra processing on plugin definitions. + * + * By default we add defaults for the type to the definition. If a type has + * additional processing logic they can do that by replacing or extending the + * method. + */ + protected function processDefinition(&$definition, $plugin_id) { + } + } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php index 44964c2..c59ed6a 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php @@ -41,8 +41,8 @@ public function __construct() { 'settings' => array(), 'default_value' => TRUE, ); - $base_discovery = new AlterDecorator(new DefaultsDecorator(new FormatterLegacyDiscoveryDecorator(new AnnotatedClassDiscovery('field', 'formatter')), $defaults), 'field_formatter_info'); - $this->discovery = new CacheDecorator($base_discovery, $this->cache_id, $this->cache_bin); + $this->baseDiscovery = new AlterDecorator(new DefaultsDecorator(new FormatterLegacyDiscoveryDecorator(new AnnotatedClassDiscovery('field', 'formatter')), $defaults), 'field_formatter_info'); + $this->discovery = new CacheDecorator($this->baseDiscovery, $this->cache_id, $this->cache_bin); $this->factory = new FormatterFactory($this); } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php index b3b0008..88b1834 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php @@ -41,8 +41,8 @@ public function __construct() { 'multiple_values' => FALSE, 'default_value' => TRUE, ); - $base_discovery = new DefaultsDecorator(new WidgetLegacyDiscoveryDecorator(new AnnotatedClassDiscovery('field', 'widget')), $defaults); - $this->discovery = new CacheDecorator($base_discovery, $this->cache_id, $this->cache_bin); + $this->baseDiscovery = new DefaultsDecorator(new WidgetLegacyDiscoveryDecorator(new AnnotatedClassDiscovery('field', 'widget')), $defaults); + $this->discovery = new CacheDecorator($this->baseDiscovery, $this->cache_id, $this->cache_bin); $this->factory = new WidgetFactory($this); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php index dcdaa34..5daf1e9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php @@ -27,14 +27,20 @@ function testInspection() { foreach (array('user_login') as $id) { $plugin = $this->testPluginManager->createInstance($id); $this->assertIdentical($plugin->getPluginId(), $id); - $this->assertIdentical($plugin->getDefinition(), $this->testPluginExpectedDefinitions[$id]); + $this->assertIdentical($this->testPluginManager->getDefinition($id), $this->testPluginExpectedDefinitions[$id]); } // Skip the 'menu' derived blocks, because MockMenuBlock does not implement // PluginInspectionInterface. The others do by extending PluginBase. foreach (array('user_login', 'layout') as $id) { $plugin = $this->mockBlockManager->createInstance($id); $this->assertIdentical($plugin->getPluginId(), $id); - $this->assertIdentical($plugin->getDefinition(), $this->mockBlockExpectedDefinitions[$id]); + $this->assertIdentical($this->mockBlockManager->getDefinition($id), $this->mockBlockExpectedDefinitions[$id]); + } + // Test a plugin manager that provides defaults. + foreach (array('test_block1', 'test_block2') as $id) { + $plugin = $this->defaultsTestPluginManager->createInstance($id); + $this->assertIdentical($plugin->getPluginId(), $id); + $this->assertIdentical($this->defaultsTestPluginManager->getDefinition($id), $this->defaultsTestPluginExpectedDefinitions[$id]); } } 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 5595e28..5db9322 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php @@ -10,6 +10,7 @@ use Drupal\simpletest\UnitTestBase; use Drupal\plugin_test\Plugin\TestPluginManager; use Drupal\plugin_test\Plugin\MockBlockManager; +use Drupal\plugin_test\Plugin\DefaultsTestPluginManager; /** * Base class for Plugin API unit tests. @@ -19,6 +20,8 @@ protected $testPluginExpectedDefinitions; protected $mockBlockManager; protected $mockBlockExpectedDefinitions; + protected $defaultsTestPluginManager; + protected $defaultsTestPluginExpectedDefinitions; public function setUp() { parent::setUp(); @@ -33,6 +36,7 @@ public function setUp() { // as derivatives and ReflectionFactory. $this->testPluginManager = new TestPluginManager(); $this->mockBlockManager = new MockBlockManager(); + $this->defaultsTestPluginManager = new DefaultsTestPluginManager(); // The expected plugin definitions within each manager. Several tests assert // that these plugins and their definitions are found and returned by the @@ -67,5 +71,22 @@ public function setUp() { 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock', ), ); + $this->defaultsTestPluginExpectedDefinitions = array( + 'test_block1' => array( + 'metadata' => array( + 'default' => TRUE, + 'custom' => TRUE, + ), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + ), + 'test_block2' => array( + 'metadata' => array( + 'default' => FALSE, + 'custom' => TRUE, + ), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + ), + ); } + } diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php new file mode 100644 index 0000000..607b100 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php @@ -0,0 +1,51 @@ +discovery = new StaticDiscovery(); + $this->factory = new DefaultFactory($this); + + // Specify default values. + $this->defaults = array( + 'metadata' => array( + 'default' => TRUE, + ), + ); + + // Add a plugin with a custom value. + $this->discovery->setDefinition('test_block1', array( + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + 'metadata' => array( + 'custom' => TRUE, + ), + )); + // Add a plugin that overrides the default value. + $this->discovery->setDefinition('test_block2', array( + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + 'metadata' => array( + 'custom' => TRUE, + 'default' => FALSE, + ), + )); + } + +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php new file mode 100644 index 0000000..63ea928 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php @@ -0,0 +1,20 @@ +