Skip to content
Snippets Groups Projects
Commit 1afb8711 authored by catch's avatar catch
Browse files

Issue #3482618 by nicxvan, godotislate: ModuleHandler->alterDeprecated does...

Issue #3482618 by nicxvan, godotislate: ModuleHandler->alterDeprecated does not properly handle deprecated OOP hooks
parent b8ec70b4
No related branches found
No related tags found
No related merge requests found
......@@ -5,8 +5,8 @@
use Drupal\Component\Graph\Graph;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\Hook\HookCollectorPass;
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\Core\Hook\HookCollectorPass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
......@@ -59,8 +59,9 @@ class ModuleHandler implements ModuleHandlerInterface {
protected $includeFileKeys = [];
/**
* Hook and module keyed list of listeners.
*
* @var array
* hook and module keyed list of listeners.
*/
protected array $invokeMap = [];
......@@ -250,7 +251,7 @@ public function loadAllIncludes($type, $name = NULL) {
*/
public function loadInclude($module, $type, $name = NULL) {
if ($type == 'install') {
// Make sure the installation API is available
// Make sure the installation API is available.
include_once $this->root . '/core/includes/install.inc';
}
......@@ -389,11 +390,8 @@ public function invokeAllDeprecated($description, $hook, array $args = []) {
private function triggerDeprecationError($description, $hook) {
$modules = array_keys($this->getHookListeners($hook));
if (!empty($modules)) {
$message = 'The deprecated hook hook_' . $hook . '() is implemented in these functions: ';
$implementations = array_map(function ($module) use ($hook) {
return $module . '_' . $hook . '()';
}, $modules);
@trigger_error($message . implode(', ', $implementations) . '. ' . $description, E_USER_DEPRECATED);
$message = 'The deprecated hook hook_' . $hook . '() is implemented in these modules: ';
@trigger_error($message . implode(', ', $modules) . '. ' . $description, E_USER_DEPRECATED);
}
}
......@@ -508,8 +506,11 @@ public function alterDeprecated($description, $type, &$data, &$context1 = NULL,
if (is_string($listener)) {
$functions[] = substr($listener, 1);
}
else {
$functions[] = get_class($listener[0]) . '::' . $listener[1];
}
}
$message = 'The deprecated alter hook hook_' . $type . '_alter() is implemented in these functions: ' . implode(', ', $functions) . '.';
$message = 'The deprecated alter hook hook_' . $type . '_alter() is implemented in these locations: ' . implode(', ', $functions) . '.';
@trigger_error($message . ' ' . $description, E_USER_DEPRECATED);
}
}
......
name: 'Deprecation test'
type: module
description: 'Support module for testing deprecation behaviors.'
package: Testing
version: VERSION
<?php
declare(strict_types=1);
namespace Drupal\deprecation_hook_attribute_test\Hook;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Implements hooks for the deprecation hook attribute test.
*/
class DeprecationHookAttributeTestHooks {
/**
* Implements hook_deprecated_hook().
*/
#[Hook('deprecated_hook')]
public function deprecatedHook($arg): mixed {
return $arg;
}
/**
* Implements hook_deprecated_alter_alter().
*/
#[Hook('deprecated_alter_alter')]
public function deprecatedAlterAlterFirst(&$data, $context1, $context2): void {
$data = [$context1, $context2];
}
}
......@@ -19,13 +19,16 @@ class ModuleHandlerDeprecatedHookTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['deprecation_test'];
protected static $modules = [
'deprecation_test',
'deprecation_hook_attribute_test',
];
/**
* @covers ::invokeDeprecated
*/
public function testInvokeDeprecated(): void {
$this->expectDeprecation('The deprecated hook hook_deprecated_hook() is implemented in these functions: deprecation_test_deprecated_hook(). Use something else.');
$this->expectDeprecation('The deprecated hook hook_deprecated_hook() is implemented in these modules: deprecation_hook_attribute_test, deprecation_test. Use something else.');
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = $this->container->get('module_handler');
$arg = 'an_arg';
......@@ -39,12 +42,15 @@ public function testInvokeDeprecated(): void {
* @covers ::invokeAllDeprecated
*/
public function testInvokeAllDeprecated(): void {
$this->expectDeprecation('The deprecated hook hook_deprecated_hook() is implemented in these functions: deprecation_test_deprecated_hook(). Use something else.');
$this->expectDeprecation('The deprecated hook hook_deprecated_hook() is implemented in these modules: deprecation_hook_attribute_test, deprecation_test. Use something else.');
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = $this->container->get('module_handler');
$arg = 'an_arg';
$this->assertEquals(
[$arg],
[
$arg,
$arg,
],
$module_handler->invokeAllDeprecated('Use something else.', 'deprecated_hook', [$arg])
);
}
......@@ -53,7 +59,7 @@ public function testInvokeAllDeprecated(): void {
* @covers ::alterDeprecated
*/
public function testAlterDeprecated(): void {
$this->expectDeprecation('The deprecated alter hook hook_deprecated_alter_alter() is implemented in these functions: deprecation_test_deprecated_alter_alter. Alter something else.');
$this->expectDeprecation('The deprecated alter hook hook_deprecated_alter_alter() is implemented in these locations: Drupal\deprecation_hook_attribute_test\Hook\DeprecationHookAttributeTestHooks::deprecatedAlterAlterFirst, deprecation_test_deprecated_alter_alter. Alter something else.');
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = $this->container->get('module_handler');
$data = [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment