diff --git a/core/lib/Drupal/Core/Archiver/ArchiverManager.php b/core/lib/Drupal/Core/Archiver/ArchiverManager.php index 84b1a3f..11ec29b 100644 --- a/core/lib/Drupal/Core/Archiver/ArchiverManager.php +++ b/core/lib/Drupal/Core/Archiver/ArchiverManager.php @@ -32,8 +32,7 @@ class ArchiverManager extends DefaultPluginManager { */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) { parent::__construct('Archiver', $namespaces); - $this->alterHook = 'archiver_info'; - $this->moduleHandler = $module_handler; + $this->alterInfo($module_handler, 'archiver_info'); $this->setCache($cache, $language_manager, 'archiver_info'); } diff --git a/core/lib/Drupal/Core/Condition/ConditionManager.php b/core/lib/Drupal/Core/Condition/ConditionManager.php index 35948a7..ead3a9c 100644 --- a/core/lib/Drupal/Core/Condition/ConditionManager.php +++ b/core/lib/Drupal/Core/Condition/ConditionManager.php @@ -34,8 +34,7 @@ class ConditionManager extends DefaultPluginManager implements ExecutableManager */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) { parent::__construct('Condition', $namespaces); - $this->alterHook = 'condition_info'; - $this->moduleHandler = $module_handler; + $this->alterInfo($module_handler, 'condition_info'); $this->setCache($cache, $language_manager, 'condition'); } diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index e04fa87..9b08432 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -124,6 +124,19 @@ public function setCache(CacheBackendInterface $cache, LanguageManager $language } /** + * Initializes the alter hook. + * + * @param \Drupal\Core\Extension\ModuleHandler $module_handler + * The module handler to invoke the alter hook with. + * @param string $alter_hook + * Name of the alter hook. + */ + protected function alterInfo(ModuleHandlerInterface $module_handler, $alter_hook) { + $this->moduleHandler = $module_handler; + $this->alterHook = $alter_hook; + } + + /** * {@inheritdoc} */ public function getDefinition($plugin_id) { diff --git a/core/lib/Drupal/Core/Validation/ConstraintManager.php b/core/lib/Drupal/Core/Validation/ConstraintManager.php index 14e44a1..ee902d4 100644 --- a/core/lib/Drupal/Core/Validation/ConstraintManager.php +++ b/core/lib/Drupal/Core/Validation/ConstraintManager.php @@ -49,8 +49,7 @@ class ConstraintManager extends DefaultPluginManager { public function __construct(\Traversable $namespaces, CacheBackendInterface $cache, LanguageManager $language_manager, ModuleHandler $module_handler) { parent::__construct('Validation/Constraint', $namespaces); $this->discovery = new StaticDiscoveryDecorator($this->discovery, array($this, 'registerDefinitions')); - $this->alterHook = 'validation_constraint'; - $this->moduleHandler = $module_handler; + $this->alterInfo($module_handler, 'validation_constraint'); $this->setCache($cache, $language_manager, 'validation_constraint'); } diff --git a/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php b/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php index 28716e6..4932daf 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php @@ -34,8 +34,7 @@ class BlockManager extends DefaultPluginManager { */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) { parent::__construct('Block', $namespaces); - $this->alterHook = 'block'; - $this->moduleHandler = $module_handler; + $this->alterInfo($module_handler, 'block'); $this->setCache($cache, $language_manager, 'block_plugins'); } } diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index 8f2e74f..07de26b 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -31,6 +31,9 @@ class DefaultPluginManagerTest extends UnitTestCase { */ protected $namespaces; + /** + * {@inheritdoc} + */ public static function getInfo() { return array( 'name' => 'Default Plugin Manager', @@ -39,6 +42,9 @@ public static function getInfo() { ); } + /** + * {@inheritdoc} + */ protected function setUp() { $this->expectedDefinitions = array( 'apple' => array( @@ -82,8 +88,7 @@ public function testDefaultPluginManagerWithAlter() { ->method('alter') ->with($this->equalTo($alter_hook_name), $this->equalTo($this->expectedDefinitions)); - $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions); - $plugin_manager->setAlterHook($module_handler, $alter_hook_name); + $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, $alter_hook_name); $this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions()); $this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana')); diff --git a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php index cc23350..746adcf 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php +++ b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php @@ -22,8 +22,12 @@ class TestPluginManager extends DefaultPluginManager { * @param \Traversable $namespaces * An object that implements \Traversable which contains the root paths * keyed by the corresponding namespace to look for plugin implementations, + * @param \Drupal\Core\Extension\ModuleHandler $module_handler + * (optional) The module handler to invoke the alter hook with. + * @param string $alter_hook + * (optional) Name of the alter hook. */ - public function __construct(\Traversable $namespaces, $definitions) { + public function __construct(\Traversable $namespaces, $definitions, ModuleHandlerInterface $module_handler = NULL, $alter_hook = NULL) { // Create the object that can be used to return definitions for all the // plugins available for this type. Most real plugin managers use a richer // discovery implementation, but StaticDiscovery lets us add some simple @@ -34,19 +38,10 @@ public function __construct(\Traversable $namespaces, $definitions) { foreach ($definitions as $key => $definition) { $this->discovery->setDefinition($key, $definition); } - } - /** - * Set the alter hook name that should be used if needed. - * - * @param \Drupal\Core\Extension\ModuleHandler $module_handler - * The module handler to invoke the alter hook with. - * @param string $alter_hook - * Name of the alter hook. Defaults to $owner_$type if not given. - */ - public function setAlterHook(ModuleHandlerInterface $module_handler, $alter_hook) { - $this->moduleHandler = $module_handler; - $this->alterHook = $alter_hook; + if ($module_handler && $alter_hook) { + $this->alterInfo($module_handler, $alter_hook); + } } }