diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index 6508be6aec7621da8a7da315978b200829c0dbcb..61e2f638fe50262aa531f76a7e248b597f77a860 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -717,6 +717,7 @@ public function getModuleDirectories() { * {@inheritdoc} */ public function getName($module) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Extension\ModuleExtensionList::getName($module) instead. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); return \Drupal::service('extension.list.module')->getName($module); } diff --git a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php index f1fb3e4a869a95754afdb2da3e784e01495bf4b8..36df82821cf77bc8de850b0dd8f987087f6606c0 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php @@ -406,6 +406,11 @@ public function getModuleDirectories(); * @return string * Returns the human readable name of the module or the machine name passed * in if no matching module is found. + * + * @deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. + * Use \Drupal::service('extension.list.module')->getName($module) instead. + * + * @see https://www.drupal.org/node/3310017 */ public function getName($module); diff --git a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php index 36b91f6b3c2021514e0d01f1c3fc6ad3d87d496f..66341b3c3aa6cfeeb182b63cba79f884fe4f37aa 100644 --- a/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php +++ b/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Menu\Form; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Menu\MenuLinkInterface; use Drupal\Core\Menu\MenuLinkManagerInterface; @@ -60,12 +61,18 @@ class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionIn * The string translation. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler; + * @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList + * The module extension list. */ - public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) { + public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, protected ?ModuleExtensionList $moduleExtensionList = NULL) { $this->menuLinkManager = $menu_link_manager; $this->menuParentSelector = $menu_parent_selector; $this->stringTranslation = $string_translation; $this->moduleHandler = $module_handler; + if ($this->moduleExtensionList === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $this->moduleExtensionList = \Drupal::service('extension.list.module'); + } } /** @@ -76,7 +83,8 @@ public static function create(ContainerInterface $container) { $container->get('plugin.manager.menu.link'), $container->get('menu.parent_form_selector'), $container->get('string_translation'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('extension.list.module') ); } @@ -96,7 +104,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $provider = $this->menuLink->getProvider(); $form['info'] = [ '#type' => 'item', - '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleHandler->getName($provider)]), + '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleExtensionList->getName($provider)]), ]; $form['id'] = [ '#type' => 'value', diff --git a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php index 1ce144680262072e4349ed569eb23116b832f884..763006bef54e099166de2e9785a6b31dccd4ee74 100644 --- a/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php +++ b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php @@ -2,6 +2,8 @@ namespace Drupal\Core\Plugin; +use Drupal\Core\Extension\Exception\UnknownExtensionException; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\StringTranslation\StringTranslationTrait; /** @@ -46,13 +48,13 @@ protected function processDefinitionCategory(&$definition) { * machine-readable name passed. */ protected function getProviderName($provider) { - $list = $this->getModuleHandler()->getModuleList(); - // If the module exists, return its human-readable name. - if (isset($list[$provider])) { - return $this->getModuleHandler()->getName($provider); + try { + return $this->getModuleExtensionList()->getName($provider); + } + catch (UnknownExtensionException $e) { + // Otherwise, return the machine name. + return $provider; } - // Otherwise, return the machine name. - return $provider; } /** @@ -60,8 +62,14 @@ protected function getProviderName($provider) { * * @return \Drupal\Core\Extension\ModuleHandlerInterface * The module handler. + * + * @deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3310017 */ public function getModuleHandler() { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); // If the class has an injected module handler, use it. Otherwise fall back // to fetch it from the service container. if (isset($this->moduleHandler)) { @@ -70,6 +78,21 @@ public function getModuleHandler() { return \Drupal::moduleHandler(); } + /** + * Returns the module extension list used. + * + * @return \Drupal\Core\Extension\ModuleExtensionList + * The module extension list. + */ + protected function getModuleExtensionList(): ModuleExtensionList { + // If the class has an injected module extension list, use it. Otherwise + // fall back to fetch it from the service container. + if (isset($this->moduleExtensionList)) { + return $this->moduleExtensionList; + } + return \Drupal::service('extension.list.module'); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 3b9036e6633706bd2830909ed5300f252f5ff884..1f62d84e4a61a32ebd221fa570fbddd77676ddaf 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -6,6 +6,7 @@ use Drupal\Component\Plugin\Attribute\AttributeInterface; use Drupal\Component\Plugin\Definition\PluginDefinitionInterface; use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\UseCacheBackendTrait; @@ -68,6 +69,13 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt */ protected $moduleHandler; + /** + * The module extension list. + * + * @var \Drupal\Core\Extension\ModuleExtensionList + */ + protected ?ModuleExtensionList $moduleExtensionList; + /** * A set of defaults to be referenced by $this->processDefinition(). * diff --git a/core/modules/breakpoint/breakpoint.services.yml b/core/modules/breakpoint/breakpoint.services.yml index 5bfcbe4111bc45a02bf6ddf1cd5df7ffcab93218..ead362d58b4205e9a1b6ad6b4a9cc176699eaa8b 100644 --- a/core/modules/breakpoint/breakpoint.services.yml +++ b/core/modules/breakpoint/breakpoint.services.yml @@ -1,7 +1,7 @@ services: breakpoint.manager: class: Drupal\breakpoint\BreakpointManager - arguments: ['@module_handler', '@theme_handler', '@cache.discovery', '@string_translation'] + arguments: ['@module_handler', '@theme_handler', '@cache.discovery', '@string_translation', '@extension.list.module'] tags: - { name: plugin_manager_cache_clear } Drupal\breakpoint\BreakpointManagerInterface: '@breakpoint.manager' diff --git a/core/modules/breakpoint/src/BreakpointManager.php b/core/modules/breakpoint/src/BreakpointManager.php index 070b0bf043842ad9194ddd8b22ded0a3e7d4f730..930e6a694a4f55ad022768ddd879f28f200eed2a 100644 --- a/core/modules/breakpoint/src/BreakpointManager.php +++ b/core/modules/breakpoint/src/BreakpointManager.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; @@ -98,11 +99,18 @@ class BreakpointManager extends DefaultPluginManager implements BreakpointManage * The cache backend. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation service. + * @param \Drupal\Core\Extension\ModuleExtensionList|null $module_extension_list + * The module extension list. */ - public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation) { + public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation, ?ModuleExtensionList $module_extension_list = NULL) { $this->factory = new ContainerFactory($this); $this->moduleHandler = $module_handler; $this->themeHandler = $theme_handler; + if ($module_extension_list === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $module_extension_list argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $module_extension_list = \Drupal::service('extension.list.module'); + } + $this->moduleExtensionList = $module_extension_list; $this->setStringTranslation($string_translation); $this->alterInfo('breakpoints'); $this->setCacheBackend($cache_backend, 'breakpoints', ['breakpoints']); @@ -244,7 +252,7 @@ public function clearCachedDefinitions() { protected function getGroupLabel($group) { // Extension names are not translatable. if ($this->moduleHandler->moduleExists($group)) { - $label = $this->moduleHandler->getName($group); + $label = $this->moduleExtensionList->getName($group); } elseif ($this->themeHandler->themeExists($group)) { $label = $this->themeHandler->getName($group); diff --git a/core/modules/field/field.module b/core/modules/field/field.module index f68daf91e3e31346f6a2b9565dae13e0c1c561f1..c5428e53b5f7f558ba630c7950a35d43a5e2458a 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -107,11 +107,12 @@ function field_help($route_name, RouteMatchInterface $route_match) { } $providers = array_unique($providers); sort($providers); + $module_extension_list = \Drupal::service('extension.list.module'); foreach ($providers as $provider) { // Skip plugins provided by core components as they do not implement // hook_help(). if (isset($modules[$provider])) { - $display = \Drupal::moduleHandler()->getName($provider); + $display = $module_extension_list->getName($provider); if (\Drupal::moduleHandler()->hasImplementations('help', $provider)) { $items[] = Link::fromTextAndUrl($display, Url::fromRoute('help.page', ['name' => $provider]))->toRenderable(); } diff --git a/core/modules/help/src/Controller/HelpController.php b/core/modules/help/src/Controller/HelpController.php index 6f5cc767c8482212bde97685396acf83bf40e360..5b33528a889d2eeba465c31be4069789e238e8c4 100644 --- a/core/modules/help/src/Controller/HelpController.php +++ b/core/modules/help/src/Controller/HelpController.php @@ -162,7 +162,7 @@ public function helpMain() { public function helpPage($name) { $build = []; if ($this->moduleHandler()->hasImplementations('help', $name)) { - $module_name = $this->moduleHandler()->getName($name); + $module_name = $this->moduleExtensionList->getName($name); $build['#title'] = $module_name; $info = $this->moduleExtensionList->getExtensionInfo($name); diff --git a/core/modules/help/src/Plugin/HelpSection/HookHelpSection.php b/core/modules/help/src/Plugin/HelpSection/HookHelpSection.php index 9336d7fde4031a3beb3e65a8257fffc289c0f1ed..58350f5b9f1bf86e5bf222bb981c22b7c9b72fdc 100644 --- a/core/modules/help/src/Plugin/HelpSection/HookHelpSection.php +++ b/core/modules/help/src/Plugin/HelpSection/HookHelpSection.php @@ -2,6 +2,7 @@ namespace Drupal\help\Plugin\HelpSection; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Link; @@ -37,10 +38,16 @@ class HookHelpSection extends HelpSectionPluginBase implements ContainerFactoryP * The plugin implementation definition. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. + * @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList + * The module extension list. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, protected ?ModuleExtensionList $moduleExtensionList = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->moduleHandler = $module_handler; + if ($this->moduleExtensionList === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $this->moduleExtensionList = \Drupal::service('extension.list.module'); + } } /** @@ -51,7 +58,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('module_handler') + $container->get('module_handler'), + $container->get('extension.list.module'), ); } @@ -63,7 +71,7 @@ public function listTopics() { $this->moduleHandler->invokeAllWith( 'help', function (callable $hook, string $module) use (&$topics) { - $title = $this->moduleHandler->getName($module); + $title = $this->moduleExtensionList->getName($module); $topics[$title] = Link::createFromRoute($title, 'help.page', ['name' => $module]); } ); diff --git a/core/modules/help/tests/src/Functional/HelpTest.php b/core/modules/help/tests/src/Functional/HelpTest.php index 57b99fe74e8fe7c3c2a99ab033ce6af9f28a673a..afcf2d04b4c64c7b3cf04b93d182241c29c2ee2b 100644 --- a/core/modules/help/tests/src/Functional/HelpTest.php +++ b/core/modules/help/tests/src/Functional/HelpTest.php @@ -97,8 +97,9 @@ public function testHelp() { // Ensure a module which does not provide a module overview page is handled // correctly. - $this->clickLink(\Drupal::moduleHandler()->getName('help_test')); - $this->assertSession()->pageTextContains('No help is available for module ' . \Drupal::moduleHandler()->getName('help_test')); + $module_name = \Drupal::service('extension.list.module')->getName('help_test'); + $this->clickLink($module_name); + $this->assertSession()->pageTextContains('No help is available for module ' . $module_name); // Verify that the order of topics is alphabetical by displayed module // name, by checking the order of some modules, including some that would diff --git a/core/modules/help/tests/src/Functional/NoHelpTest.php b/core/modules/help/tests/src/Functional/NoHelpTest.php index 14999e29979a2fe3d3666417784dd849c32b95aa..8252583f182d3d208c7c9402a7dcf2a2178e689b 100644 --- a/core/modules/help/tests/src/Functional/NoHelpTest.php +++ b/core/modules/help/tests/src/Functional/NoHelpTest.php @@ -52,7 +52,7 @@ public function testMainPageNoHelp() { $this->assertFalse(\Drupal::moduleHandler()->hasImplementations('help', 'menu_test'), 'The menu_test module does not implement hook_help'); // Make sure the test module menu_test does not display a help link on // admin/help. - $this->assertSession()->pageTextNotContains(\Drupal::moduleHandler()->getName('menu_test')); + $this->assertSession()->pageTextNotContains(\Drupal::service('extension.list.module')->getName('menu_test')); // Ensure that the module overview help page for a module that does not // implement hook_help() results in a 404. diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 7629cbf38c2a6fcfc2a61094e865205e4ea4f021..99453170239319b574eac31c64c65386c16a27e5 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -418,6 +418,7 @@ function language_entity_field_access($operation, FieldDefinitionInterface $fiel * Implements hook_tour_tips_alter(). */ function language_tour_tips_alter(array &$tour_tips, EntityInterface $entity) { + $module_extension_list = \Drupal::service('extension.list.module'); foreach ($tour_tips as $tour_tip) { if ($tour_tip->get('id') == 'language-overview') { $additional_overview = ''; @@ -433,10 +434,10 @@ function language_tour_tips_alter(array &$tour_tips, EntityInterface $entity) { $additional_continue = ''; $additional_modules = []; if (!Drupal::service('module_handler')->moduleExists('locale')) { - $additional_modules[] = Drupal::service('module_handler')->getName('locale'); + $additional_modules[] = $module_extension_list->getName('locale'); } if (!Drupal::service('module_handler')->moduleExists('content_translation')) { - $additional_modules[] = Drupal::service('module_handler')->getName('content_translation'); + $additional_modules[] = $module_extension_list->getName('content_translation'); } if (!empty($additional_modules)) { $additional_continue = t('Depending on your site features, additional modules that you might want to install are:') . '<ul>'; diff --git a/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php b/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php index 3af6ac1202d3482f61a15bd41cbd1379d78b67c2..336734d0c4761e505937cbf833dae07d447d8aa1 100644 --- a/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php @@ -5,7 +5,9 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; use Drupal\Core\Extension\Exception\UnknownExtensionException; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\State\StateInterface; @@ -33,6 +35,12 @@ * @internal */ class ReviewForm extends MigrateUpgradeFormBase { + use DeprecatedServicePropertyTrait; + + /** + * The service properties that should raise a deprecation error. + */ + private array $deprecatedProperties = ['moduleHandler' => 'module_handler']; /** * The migrations. @@ -49,11 +57,9 @@ class ReviewForm extends MigrateUpgradeFormBase { protected $migrationState; /** - * Module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface + * Module extension list. */ - protected $moduleHandler; + protected ModuleExtensionList $moduleExtensionList; /** * Source system data set in buildForm(). @@ -75,8 +81,8 @@ class ReviewForm extends MigrateUpgradeFormBase { * Migration state service. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. + * @param \Drupal\Core\Extension\ModuleExtensionList|\Drupal\Core\Extension\ModuleHandlerInterface $module_extension_list + * The module extension list. * @param \Drupal\Component\Datetime\TimeInterface|null $time * The time service. */ @@ -86,16 +92,20 @@ public function __construct( PrivateTempStoreFactory $tempstore_private, MigrationState $migrationState, ConfigFactoryInterface $config_factory, - ModuleHandlerInterface $module_handler, + ModuleExtensionList|ModuleHandlerInterface $module_extension_list, protected ?TimeInterface $time = NULL, ) { parent::__construct($config_factory, $migration_plugin_manager, $state, $tempstore_private); $this->migrationState = $migrationState; - $this->moduleHandler = $module_handler; if ($this->time === NULL) { @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3112298', E_USER_DEPRECATED); $this->time = \Drupal::service('datetime.time'); } + if ($module_extension_list instanceof ModuleHandlerInterface) { + @trigger_error('Calling ' . __METHOD__ . '() with the $module_extension_list argument as ModuleHandlerInterface is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $module_extension_list = \Drupal::service('extension.list.module'); + } + $this->moduleExtensionList = $module_extension_list; } /** @@ -108,7 +118,7 @@ public static function create(ContainerInterface $container) { $container->get('tempstore.private'), $container->get('migrate_drupal.migration_state'), $container->get('config.factory'), - $container->get('module_handler'), + $container->get('extension.list.module'), $container->get('datetime.time'), ); } @@ -311,7 +321,7 @@ protected function prepareOutput(array $migration_state) { } else { try { - $destination_module_names[] = $this->moduleHandler->getName($destination_module); + $destination_module_names[] = $this->moduleExtensionList->getName($destination_module); } catch (UnknownExtensionException $e) { $destination_module_names[] = $destination_module; diff --git a/core/modules/user/src/Form/EntityPermissionsForm.php b/core/modules/user/src/Form/EntityPermissionsForm.php index 126eb7b975b8067a1e63ab1802d3f77e32e958f9..48cd436145a52fa3377cddf4ddceb00b2d6cfbdd 100644 --- a/core/modules/user/src/Form/EntityPermissionsForm.php +++ b/core/modules/user/src/Form/EntityPermissionsForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigManagerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -58,9 +59,15 @@ class EntityPermissionsForm extends UserPermissionsForm { * The configuration entity manager. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. + * @param \Drupal\Core\Extension\ModuleExtensionList|null $module_extension_list + * The module extension list. */ - public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) { - parent::__construct($permission_handler, $role_storage, $module_handler); + public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager, ?ModuleExtensionList $module_extension_list = NULL) { + if ($module_extension_list === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $module_extension_list argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $module_extension_list = \Drupal::service('extension.list.module'); + } + parent::__construct($permission_handler, $role_storage, $module_handler, $module_extension_list); $this->configManager = $config_manager; $this->entityTypeManager = $entity_type_manager; } @@ -74,7 +81,8 @@ public static function create(ContainerInterface $container) { $container->get('entity_type.manager')->getStorage('user_role'), $container->get('module_handler'), $container->get('config.manager'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('extension.list.module'), ); } diff --git a/core/modules/user/src/Form/UserPermissionsForm.php b/core/modules/user/src/Form/UserPermissionsForm.php index d1e9b9932657d17ddd33278ea7d4ecc29edfcb49..8874b2661417851281d9d54d41e47c7d724f7e22 100644 --- a/core/modules/user/src/Form/UserPermissionsForm.php +++ b/core/modules/user/src/Form/UserPermissionsForm.php @@ -2,6 +2,7 @@ namespace Drupal\user\Form; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; @@ -46,11 +47,17 @@ class UserPermissionsForm extends FormBase { * The role storage. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList + * The module extension list. */ - public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler) { + public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, protected ?ModuleExtensionList $moduleExtensionList = NULL) { $this->permissionHandler = $permission_handler; $this->roleStorage = $role_storage; $this->moduleHandler = $module_handler; + if ($this->moduleExtensionList === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $this->moduleExtensionList = \Drupal::service('extension.list.module'); + } } /** @@ -60,7 +67,8 @@ public static function create(ContainerInterface $container) { return new static( $container->get('user.permissions'), $container->get('entity_type.manager')->getStorage('user_role'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('extension.list.module'), ); } @@ -186,7 +194,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'class' => ['module'], 'id' => 'module-' . $provider, ], - '#markup' => $this->moduleHandler->getName($provider), + '#markup' => $this->moduleExtensionList->getName($provider), ], ]; foreach ($permissions as $perm => $perm_item) { diff --git a/core/modules/user/src/PermissionHandler.php b/core/modules/user/src/PermissionHandler.php index 50670b038133404f8250f1ea91e85c6405be6dfb..5858b33e2a08607f9916c45489e160a73adef388 100644 --- a/core/modules/user/src/PermissionHandler.php +++ b/core/modules/user/src/PermissionHandler.php @@ -4,6 +4,7 @@ use Drupal\Core\Discovery\YamlDiscovery; use Drupal\Core\Controller\ControllerResolverInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; @@ -84,8 +85,10 @@ class PermissionHandler implements PermissionHandlerInterface { * The string translation. * @param \Drupal\Core\Utility\CallableResolver|\Drupal\Core\Controller\ControllerResolverInterface $callable_resolver * The callable resolver. + * @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList + * The module extension list. */ - public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface|CallableResolver $callable_resolver) { + public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface|CallableResolver $callable_resolver, protected ?ModuleExtensionList $moduleExtensionList = NULL) { if ($callable_resolver instanceof ControllerResolverInterface) { @trigger_error('Calling ' . __METHOD__ . '() with an argument of ControllerResolverInterface is deprecated in drupal:10.2.0 and is removed in drupal:11.0.0. Use \Drupal\Core\Utility\CallableResolver instead. See https://www.drupal.org/node/3397954', E_USER_DEPRECATED); $callable_resolver = \Drupal::service('callable_resolver'); @@ -96,6 +99,10 @@ public function __construct(ModuleHandlerInterface $module_handler, TranslationI // container. $this->moduleHandler = $module_handler; $this->stringTranslation = $string_translation; + if ($this->moduleExtensionList === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $this->moduleExtensionList = \Drupal::service('extension.list.module'); + } } /** @@ -233,7 +240,7 @@ protected function sortPermissions(array $all_permissions = []) { protected function getModuleNames() { $modules = []; foreach (array_keys($this->moduleHandler->getModuleList()) as $module) { - $modules[$module] = $this->moduleHandler->getName($module); + $modules[$module] = $this->moduleExtensionList->getName($module); } asort($modules); return $modules; diff --git a/core/modules/user/src/Plugin/views/access/Permission.php b/core/modules/user/src/Plugin/views/access/Permission.php index 291eb1ca8edf5ee82d3b83af49232777812eb850..8f9d6457453e794ce2901fbe8a99e1743d1d4507 100644 --- a/core/modules/user/src/Plugin/views/access/Permission.php +++ b/core/modules/user/src/Plugin/views/access/Permission.php @@ -4,6 +4,8 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -24,6 +26,12 @@ * ) */ class Permission extends AccessPluginBase implements CacheableDependencyInterface { + use DeprecatedServicePropertyTrait; + + /** + * The service properties that should raise a deprecation error. + */ + private array $deprecatedProperties = ['moduleHandler' => 'module_handler']; /** * {@inheritdoc} @@ -38,11 +46,9 @@ class Permission extends AccessPluginBase implements CacheableDependencyInterfac protected $permissionHandler; /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface + * Module extension list. */ - protected $moduleHandler; + protected ModuleExtensionList $moduleExtensionList; /** * Constructs a Permission object. @@ -55,13 +61,17 @@ class Permission extends AccessPluginBase implements CacheableDependencyInterfac * The plugin implementation definition. * @param \Drupal\user\PermissionHandlerInterface $permission_handler * The permission handler. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. + * @param \Drupal\Core\Extension\ModuleExtensionList|\Drupal\Core\Extension\ModuleHandlerInterface $module_extension_list + * The module extension list. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleExtensionList|ModuleHandlerInterface $module_extension_list) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->permissionHandler = $permission_handler; - $this->moduleHandler = $module_handler; + if ($module_extension_list instanceof ModuleHandlerInterface) { + @trigger_error('Calling ' . __METHOD__ . '() with the $module_extension_list argument as ModuleHandlerInterface is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $module_extension_list = \Drupal::service('extension.list.module'); + } + $this->moduleExtensionList = $module_extension_list; } /** @@ -73,7 +83,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('user.permissions'), - $container->get('module_handler') + $container->get('extension.list.module'), ); } @@ -114,7 +124,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; - $display_name = $this->moduleHandler->getName($provider); + $display_name = $this->moduleExtensionList->getName($provider); $perms[$display_name][$perm] = strip_tags($perm_item['title']); } diff --git a/core/modules/user/src/Plugin/views/field/UserData.php b/core/modules/user/src/Plugin/views/field/UserData.php index 1a4bb82acdd5e91e778400f0cbd2acb0e9cc3b22..fb853cfc0a33ceb925d069e6ee7ec139a10fb6fa 100644 --- a/core/modules/user/src/Plugin/views/field/UserData.php +++ b/core/modules/user/src/Plugin/views/field/UserData.php @@ -2,6 +2,7 @@ namespace Drupal\user\Plugin\views\field; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\field\FieldPluginBase; @@ -38,17 +39,28 @@ class UserData extends FieldPluginBase { * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container->get('user.data'), $container->get('module_handler')); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('user.data'), + $container->get('module_handler'), + $container->get('extension.list.module') + ); } /** * Constructs a UserData object. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, ModuleHandlerInterface $module_handler) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, ModuleHandlerInterface $module_handler, protected ?ModuleExtensionList $moduleExtensionList = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->userData = $user_data; $this->moduleHandler = $module_handler; + if ($this->moduleExtensionList === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $this->moduleExtensionList = \Drupal::service('extension.list.module'); + } } /** @@ -72,7 +84,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $modules = $this->moduleHandler->getModuleList(); $names = []; foreach (array_keys($modules) as $name) { - $names[$name] = $this->moduleHandler->getName($name); + $names[$name] = $this->moduleExtensionList->getName($name); } $form['data_module'] = [ diff --git a/core/modules/user/src/Plugin/views/filter/Permissions.php b/core/modules/user/src/Plugin/views/filter/Permissions.php index b4d93af2f19cb8627f35ca5174c97bbaf046d2b2..e530de59d5701b1fc834b184d2ff29a86d85b963 100644 --- a/core/modules/user/src/Plugin/views/filter/Permissions.php +++ b/core/modules/user/src/Plugin/views/filter/Permissions.php @@ -3,6 +3,8 @@ namespace Drupal\user\Plugin\views\filter; use Drupal\Component\Utility\Html; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\user\Entity\Role; use Drupal\user\PermissionHandlerInterface; @@ -18,6 +20,12 @@ * @ViewsFilter("user_permissions") */ class Permissions extends ManyToOne { + use DeprecatedServicePropertyTrait; + + /** + * The service properties that should raise a deprecation error. + */ + private array $deprecatedProperties = ['moduleHandler' => 'module_handler']; /** * The permission handler. @@ -27,11 +35,9 @@ class Permissions extends ManyToOne { protected $permissionHandler; /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface + * Module extension list. */ - protected $moduleHandler; + protected ModuleExtensionList $moduleExtensionList; /** * Constructs a Permissions object. @@ -44,14 +50,18 @@ class Permissions extends ManyToOne { * The plugin implementation definition. * @param \Drupal\user\PermissionHandlerInterface $permission_handler * The permission handler. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. + * @param \Drupal\Core\Extension\ModuleExtensionList|\Drupal\Core\Extension\ModuleHandlerInterface $module_extension_list + * The module extension list. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleExtensionList|ModuleHandlerInterface $module_extension_list) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->permissionHandler = $permission_handler; - $this->moduleHandler = $module_handler; + if ($module_extension_list instanceof ModuleHandlerInterface) { + @trigger_error('Calling ' . __METHOD__ . '() with the $module_extension_list argument as ModuleHandlerInterface is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED); + $module_extension_list = \Drupal::service('extension.list.module'); + } + $this->moduleExtensionList = $module_extension_list; } /** @@ -63,7 +73,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('user.permissions'), - $container->get('module_handler') + $container->get('extension.list.module'), ); } @@ -72,7 +82,7 @@ public function getValueOptions() { $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; - $display_name = $this->moduleHandler->getName($provider); + $display_name = $this->moduleExtensionList->getName($provider); $this->valueOptions[$display_name][$perm] = Html::escape(strip_tags($perm_item['title'])); } return $this->valueOptions; diff --git a/core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php b/core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php index d8eb84c6aeaadcfcff70861607709d7ef69bd206..bccc105552f3294ce14648e04307b2717ec29f05 100644 --- a/core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php +++ b/core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Tests\UnitTestCase; @@ -57,6 +58,7 @@ public function testPermissionsByProvider(string $dependency_name, bool $found) $permission_handler = $prophecy->reveal(); $role_storage = $this->prophesize(RoleStorageInterface::class)->reveal(); $module_handler = $this->prophesize(ModuleHandlerInterface::class)->reveal(); + $module_extension_list = $this->prophesize(ModuleExtensionList::class)->reveal(); $prophecy = $this->prophesize(ConfigManagerInterface::class); $prophecy->getConfigEntitiesToChangeOnDependencyRemoval('config', ['node.type.article']) ->willReturn([ @@ -75,7 +77,7 @@ public function testPermissionsByProvider(string $dependency_name, bool $found) ->willReturn($entity_type); $entity_type_manager = $prophecy->reveal(); - $bundle_form = new EntityPermissionsForm($permission_handler, $role_storage, $module_handler, $config_manager, $entity_type_manager); + $bundle_form = new EntityPermissionsForm($permission_handler, $role_storage, $module_handler, $config_manager, $entity_type_manager, $module_extension_list); // Mock the method parameters. $route = new Route('some.path'); diff --git a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php index 114cdf0de035a6331e5d1ee7b9dcc7e89ab6ea33..fb71401294d2ed01e6bd0597bcda86e9f936d302 100644 --- a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php +++ b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\user\Unit; use Drupal\Core\Extension\Extension; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslationInterface; @@ -20,6 +21,7 @@ * @group user * * @coversDefaultClass \Drupal\user\PermissionHandler + * @runTestsInSeparateProcesses */ class PermissionHandlerTest extends UnitTestCase { @@ -130,7 +132,9 @@ public function testBuildPermissionsYaml() { $this->callableResolver->expects($this->never()) ->method('getCallableFromDefinition'); - $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver); + $module_extension_list = $this->createMock(ModuleExtensionList::class); + + $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver, $module_extension_list); $actual_permissions = $this->permissionHandler->getPermissions(); $this->assertPermissions($actual_permissions); @@ -162,7 +166,8 @@ public function testBuildPermissionsSortPerModule() { 'module_b' => vfsStream::url('modules/module_b'), 'module_c' => vfsStream::url('modules/module_c'), ]); - $this->moduleHandler->expects($this->exactly(3)) + $module_extension_list = $this->createMock(ModuleExtensionList::class); + $module_extension_list->expects($this->exactly(3)) ->method('getName') ->willReturnMap([ ['module_a', 'Module a'], @@ -191,7 +196,7 @@ public function testBuildPermissionsSortPerModule() { ->method('getModuleList') ->willReturn(array_flip($modules)); - $permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver); + $permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver, $module_extension_list); $actual_permissions = $permissionHandler->getPermissions(); $this->assertEquals(['access_module_a4', 'access_module_a1', 'access_module_a2', 'access_module_a3'], array_keys($actual_permissions)); @@ -254,7 +259,9 @@ public function testBuildPermissionsYamlCallback() { ['Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess', [new TestPermissionCallbacks(), 'titleDescriptionRestrictAccess']], ]); - $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver); + $module_extension_list = $this->createMock(ModuleExtensionList::class); + + $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver, $module_extension_list); $actual_permissions = $this->permissionHandler->getPermissions(); $this->assertPermissions($actual_permissions); @@ -297,7 +304,9 @@ public function testPermissionsYamlStaticAndCallback() { ->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription') ->willReturn([new TestPermissionCallbacks(), 'titleDescription']); - $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver); + $module_extension_list = $this->createMock(ModuleExtensionList::class); + + $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver, $module_extension_list); $actual_permissions = $this->permissionHandler->getPermissions(); diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 28f7a766f0641a7902c7516210798442992625ea..fb0389dd973e50173ef3c7468b57b3d932f1a0e7 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -48,7 +48,7 @@ services: Drupal\user\UserAuthInterface: '@user.auth' user.permissions: class: Drupal\user\PermissionHandler - arguments: ['@module_handler', '@string_translation', '@callable_resolver'] + arguments: ['@module_handler', '@string_translation', '@callable_resolver', '@extension.list.module'] Drupal\user\PermissionHandlerInterface: '@user.permissions' user.current_user_context: class: Drupal\user\ContextProvider\CurrentUserContext diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php index 17e6c47dee0e606647f2ba2b03b2c2d00af6b4ed..d70cf621e3147554c884a3d987dda4e9968e9df2 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTest.php @@ -134,6 +134,7 @@ public function testInstalled() { $database = Database::getConnection(); $module = $database->getProvider(); $module_handler = \Drupal::service('module_handler'); + $module_extension_list = \Drupal::service('extension.list.module'); // Ensure the update module is not installed. $this->assertFalse($module_handler->moduleExists('update'), 'The Update module is not installed.'); @@ -148,7 +149,7 @@ public function testInstalled() { $this->fail("Uninstalled $module module."); } catch (ModuleUninstallValidatorException $e) { - $module_name = $module_handler->getName($module); + $module_name = $module_extension_list->getName($module); $driver = $database->driver(); $this->assertStringContainsString("The module '$module_name' is providing the database driver '$driver'.", $e->getMessage()); } diff --git a/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php b/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php index b5b877fc8fed96f3939c7c083563e903c7ec3cb0..7284c06bc9806cff263b52032cc8e561af54b800 100644 --- a/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php @@ -21,7 +21,17 @@ public function testInvalidGetName() { $this->expectException(UnknownExtensionException::class); $this->expectExceptionMessage('The module module_nonsense does not exist.'); $module_handler = $this->container->get('module_handler'); - $module_handler->getName('module_nonsense'); + $module_handler->getModule('module_nonsense'); + } + + /** + * Tests deprecation of getName() function. + * + * @group legacy + */ + public function testGetNameDeprecation() { + $this->expectDeprecation('Drupal\Core\Extension\ModuleHandler::getName() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Extension\ModuleExtensionList::getName($module) instead. See https://www.drupal.org/node/3310017'); + $this->assertNotNull(\Drupal::service('module_handler')->getName('module_test')); } } diff --git a/core/tests/Drupal/Tests/Core/Menu/MenuLinkDefaultFormTest.php b/core/tests/Drupal/Tests/Core/Menu/MenuLinkDefaultFormTest.php index 60176e4d1ff3ec5b68cf028f62b1ec28565979d1..f1ab40a3bfe0b7f7a793fdf45de8a2caef2cf8b8 100644 --- a/core/tests/Drupal/Tests/Core/Menu/MenuLinkDefaultFormTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/MenuLinkDefaultFormTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\Core\Menu; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Form\FormState; use Drupal\Core\Menu\Form\MenuLinkDefaultForm; use Drupal\Core\Menu\MenuLinkDefault; @@ -16,6 +17,7 @@ /** * @coversDefaultClass \Drupal\Core\Menu\Form\MenuLinkDefaultForm * @group Menu + * @runTestsInSeparateProcesses */ class MenuLinkDefaultFormTest extends UnitTestCase { @@ -26,7 +28,8 @@ public function testExtractFormValues() { $menu_link_manager = $this->prophesize(MenuLinkManagerInterface::class); $menu_parent_form_selector = $this->prophesize(MenuParentFormSelectorInterface::class); $module_handler = $this->prophesize(ModuleHandlerInterface::class); - $menu_link_form = new MenuLinkDefaultForm($menu_link_manager->reveal(), $menu_parent_form_selector->reveal(), $this->getStringTranslationStub(), $module_handler->reveal()); + $module_extension_list = $this->prophesize(ModuleExtensionList::class); + $menu_link_form = new MenuLinkDefaultForm($menu_link_manager->reveal(), $menu_parent_form_selector->reveal(), $this->getStringTranslationStub(), $module_handler->reveal(), $module_extension_list->reveal()); $static_override = $this->prophesize(StaticMenuLinkOverridesInterface::class); $menu_link = new MenuLinkDefault([], 'my_plugin_id', [], $static_override->reveal()); diff --git a/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php b/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php index 967ac68bb09864cd28b7bb9fb1f64e48065501f3..6ea9efd4ea2f71e0562b4bc4cf14b7c2626b4612 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/CategorizingPluginManagerTraitTest.php @@ -5,6 +5,8 @@ namespace Drupal\Tests\Core\Plugin; use Drupal\Component\Plugin\CategorizingPluginManagerInterface; +use Drupal\Core\Extension\Exception\UnknownExtensionException; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\CategorizingPluginManagerTrait; use Drupal\Core\Plugin\DefaultPluginManager; @@ -13,6 +15,7 @@ /** * @coversDefaultClass \Drupal\Core\Plugin\CategorizingPluginManagerTrait * @group Plugin + * @runTestsInSeparateProcesses */ class CategorizingPluginManagerTraitTest extends UnitTestCase { @@ -33,12 +36,16 @@ protected function setUp(): void { $module_handler->expects($this->any()) ->method('getModuleList') ->willReturn(['node' => []]); - $module_handler->expects($this->any()) + $module_extension_list = $this->createMock(ModuleExtensionList::class); + $module_extension_list->expects($this->any()) ->method('getName') - ->with('node') - ->willReturn('Node'); - - $this->pluginManager = new CategorizingPluginManager($module_handler); + ->willReturnCallback(function ($argument) { + if ($argument == 'node') { + return 'Node'; + } + throw new UnknownExtensionException(); + }); + $this->pluginManager = new CategorizingPluginManager($module_handler, $module_extension_list); $this->pluginManager->setStringTranslation($this->getStringTranslationStub()); } @@ -112,11 +119,14 @@ class CategorizingPluginManager extends DefaultPluginManager implements Categori /** * Replace the constructor so we can instantiate a stub. * - * @param \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject $module_handler + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list + * The module extension list. */ - public function __construct(ModuleHandlerInterface $module_handler) { + public function __construct(ModuleHandlerInterface $module_handler, ModuleExtensionList $module_extension_list) { $this->moduleHandler = $module_handler; + $this->moduleExtensionList = $module_extension_list; } /**