diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index c3e841d9af..7598c8a284 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -439,7 +439,7 @@ public function invokeAllDeprecated($hook, array $args = array()) { private function triggerDeprecationError($hook) { $modules = array_keys($this->getImplementationInfo($hook)); if (!empty($modules)) { - @trigger_error('The following modules implement the deprecated hook hook_' . $hook . ': ' . implode(', ', $modules), E_USER_DEPRECATED); + @trigger_error('The following modules implement the deprecated hook hook_' . $hook . '(): ' . implode(', ', $modules), E_USER_DEPRECATED); } } @@ -535,6 +535,28 @@ public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) { } /** + * {@inheritdoc} + */ + public function alterDeprecated($type, &$data, &$context1 = NULL, &$context2 = NULL) { + // Invoke the alter hook. This has the side effect of populating + // $this->alterFunctions. + $this->alter($type, $data, $context1, $context2); + // The $type parameter can be an array. alter() will deal with this + // internally, but we have to extract the proper $cid in order to discover + // implementations. + $cid = $type; + if (is_array($type)) { + $cid = implode(',', $type); + $extra_types = $type; + $type = array_shift($extra_types); + } + if (!empty($this->alterFunctions[$cid])) { + $message = 'The deprecated alter hook hook_' . $type . '_alter() is implemented: ' . implode(', ', $this->alterFunctions[$cid]); + @trigger_error($message, E_USER_DEPRECATED); + } + } + + /** * Provides information about modules' implementations of a hook. * * @param string $hook diff --git a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php index c2cbc29afe..eadeebfad7 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php @@ -333,6 +333,60 @@ public function invokeAllDeprecated($hook, array $args = []); public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL); /** + * Passes alterable variables to deprecated hook_TYPE_alter() implementations. + * + * This method triggers an E_USER_DEPRECATED error if any implementations of + * the alter hook are found. It is otherwise identical to alter(). + * + * This dispatch function hands off the passed-in variables to type-specific + * hook_TYPE_alter() implementations in modules. It ensures a consistent + * interface for all altering operations. + * + * A maximum of 2 alterable arguments is supported. In case more arguments need + * to be passed and alterable, modules provide additional variables assigned by + * reference in the last $context argument: + * @code + * $context = array( + * 'alterable' => &$alterable, + * 'unalterable' => $unalterable, + * 'foo' => 'bar', + * ); + * $this->alter('mymodule_data', $alterable1, $alterable2, $context); + * @endcode + * + * Note that objects are always passed by reference in PHP5. If it is absolutely + * required that no implementation alters a passed object in $context, then an + * object needs to be cloned: + * @code + * $context = array( + * 'unalterable_object' => clone $object, + * ); + * $this->alter('mymodule_data', $data, $context); + * @endcode + * + * @param string|array $type + * A string describing the type of the alterable $data. 'form', 'links', + * 'node_content', and so on are several examples. Alternatively can be an + * array, in which case hook_TYPE_alter() is invoked for each value in the + * array, ordered first by module, and then for each module, in the order of + * values in $type. For example, when Form API is using $this->alter() to + * execute both hook_form_alter() and hook_form_FORM_ID_alter() + * implementations, it passes array('form', 'form_' . $form_id) for $type. + * @param mixed $data + * The variable that will be passed to hook_TYPE_alter() implementations to be + * altered. The type of this variable depends on the value of the $type + * argument. For example, when altering a 'form', $data will be a structured + * array. When altering a 'profile', $data will be an object. + * @param mixed $context1 + * (optional) An additional variable that is passed by reference. + * @param mixed $context2 + * (optional) An additional variable that is passed by reference. If more + * context needs to be provided to implementations, then this should be an + * associative array as described above. + */ + public function alterDeprecated($type, &$data, &$context1 = NULL, &$context2 = NULL); + + /** * Returns an array of directories for all enabled modules. Useful for * tasks such as finding a file that exists in all module directories. * diff --git a/core/modules/system/tests/modules/deprecation_test/deprecation_test.info.yml b/core/modules/system/tests/modules/deprecation_test/deprecation_test.info.yml new file mode 100644 index 0000000000..b3c6322fea --- /dev/null +++ b/core/modules/system/tests/modules/deprecation_test/deprecation_test.info.yml @@ -0,0 +1,6 @@ +name: 'Deprecation Test' +type: module +description: 'Implements deprecated hooks.' +core: 8.x +package: Testing +version: VERSION diff --git a/core/modules/system/tests/modules/deprecation_test/deprecation_test.module b/core/modules/system/tests/modules/deprecation_test/deprecation_test.module new file mode 100644 index 0000000000..64bbe166dc --- /dev/null +++ b/core/modules/system/tests/modules/deprecation_test/deprecation_test.module @@ -0,0 +1,14 @@ +container->get('module_handler'); + $data = ['some', 'data']; + $module_handler->alterDeprecated('deprecate', $data); + $this->assertContains('altered!', $data); + } + + /** + * @covers ::alterDeprecated + * @expectedDeprecation The following modules implement the deprecated hook hook_deprecate(): deprecation_test + */ + public function testInvokeDeprecated() { + /* @var $module_handler \Drupal\Core\Extension\ModuleHandler */ + $module_handler = $this->container->get('module_handler'); + $this->assertEquals( + 'hooked!', + $module_handler->invokeDeprecated('deprecation_test', 'deprecate') + ); + } + + /** + * @covers ::alterDeprecated + * @expectedDeprecation The following modules implement the deprecated hook hook_deprecate(): deprecation_test + */ + public function testInvokeAllDeprecated() { + /* @var $module_handler \Drupal\Core\Extension\ModuleHandler */ + $module_handler = $this->container->get('module_handler'); + // The invokeAll() method returns an array of values from various + // implementations, so we have to find our known value in the result array. + $this->assertContains('hooked!', $module_handler->invokeAllDeprecated('deprecate'), true); + } + +}