diff --git a/core/lib/Drupal/Core/Extension/ExtensionList.php b/core/lib/Drupal/Core/Extension/ExtensionList.php index a6c28a49a06e15c2fed91a69f9b2c51c547bb85f..dd538d1c9198dd24ab426cc2282a154d4257d0d6 100644 --- a/core/lib/Drupal/Core/Extension/ExtensionList.php +++ b/core/lib/Drupal/Core/Extension/ExtensionList.php @@ -580,4 +580,20 @@ public function checkIncompatibility($name) { return $extension->info['core_incompatible'] || (isset($extension->info['php']) && version_compare(phpversion(), $extension->info['php']) < 0); } + /** + * Array sorting callback; sorts extensions by their name. + * + * @param \Drupal\Core\Extension\Extension $a + * The first extension to compare. + * @param \Drupal\Core\Extension\Extension $b + * The second extension to compare. + * + * @return int + * Less than 0 if $a is less than $b, more than 0 if $a is greater than $b, + * and 0 if they are equal. + */ + public static function sortByName(Extension $a, Extension $b): int { + return strcasecmp($a->info['name'], $b->info['name']); + } + } diff --git a/core/modules/system/src/Controller/AdminController.php b/core/modules/system/src/Controller/AdminController.php index 90b2d1160f54a19ada36e5aec09382da48ef7b5c..3af437623bd37b724976ccce93ab02f91af56d6e 100644 --- a/core/modules/system/src/Controller/AdminController.php +++ b/core/modules/system/src/Controller/AdminController.php @@ -44,18 +44,14 @@ public static function create(ContainerInterface $container) { * A render array containing the listing. */ public function index() { - $module_info = $this->moduleExtensionList->getAllInstalledInfo(); - foreach ($module_info as $module => $info) { - $module_info[$module] = new \stdClass(); - $module_info[$module]->info = $info; - } + $extensions = array_intersect_key($this->moduleExtensionList->getList(), $this->moduleHandler()->getModuleList()); - uasort($module_info, 'system_sort_by_info_name'); + uasort($extensions, [ModuleExtensionList::class, 'sortByName']); $menu_items = []; - foreach ($module_info as $module => $info) { + foreach ($extensions as $module => $extension) { // Only display a section if there are any available tasks. - if ($admin_tasks = system_get_module_admin_tasks($module, $info->info)) { + if ($admin_tasks = system_get_module_admin_tasks($module, $extension->info)) { // Sort links by title. uasort($admin_tasks, ['\Drupal\Component\Utility\SortArray', 'sortByTitleElement']); // Move 'Configure permissions' links to the bottom of each section. @@ -66,7 +62,7 @@ public function index() { $admin_tasks[$permission_key] = $permission_task; } - $menu_items[$info->info['name']] = [$info->info['description'], $admin_tasks]; + $menu_items[$extension->info['name']] = [$extension->info['description'], $admin_tasks]; } } diff --git a/core/modules/system/src/Controller/SystemController.php b/core/modules/system/src/Controller/SystemController.php index 848af4a8d3de9a749e1bfa658a7310bc74c44b4a..41e351faf81b714a4daa1e63beb65714872fdf56 100644 --- a/core/modules/system/src/Controller/SystemController.php +++ b/core/modules/system/src/Controller/SystemController.php @@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Extension\ModuleDependencyMessageTrait; use Drupal\Core\Extension\ModuleExtensionList; +use Drupal\Core\Extension\ThemeExtensionList; use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Menu\MenuLinkTreeInterface; @@ -203,7 +204,7 @@ public function themesPage() { $config = $this->config('system.theme'); // Get all available themes. $themes = $this->themeHandler->rebuildThemeData(); - uasort($themes, 'system_sort_by_info_name'); + uasort($themes, [ThemeExtensionList::class, 'sortByName']); $theme_default = $config->get('default'); $theme_groups = ['installed' => [], 'uninstalled' => []]; diff --git a/core/modules/system/src/Form/ModulesListForm.php b/core/modules/system/src/Form/ModulesListForm.php index 58bf8b6b4de6f88f1c3708e59f93b13400c2fff6..6c0094e7913bba4765206813ca4723c089e94096 100644 --- a/core/modules/system/src/Form/ModulesListForm.php +++ b/core/modules/system/src/Form/ModulesListForm.php @@ -170,7 +170,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { // The module list needs to be reset so that it can re-scan and include // any new modules that may have been added directly into the filesystem. $modules = $this->moduleExtensionList->reset()->getList(); - uasort($modules, 'system_sort_by_info_name'); + uasort($modules, [ModuleExtensionList::class, 'sortByName']); } catch (InfoParserException $e) { $this->messenger()->addError($this->t('Modules could not be listed due to an error: %error', ['%error' => $e->getMessage()])); diff --git a/core/modules/system/src/Form/ModulesUninstallForm.php b/core/modules/system/src/Form/ModulesUninstallForm.php index 0b4374037641a619aa98a3372ac1434fe24377ab..0719511e8e2139a5b12036738212cf5cfe4bab01 100644 --- a/core/modules/system/src/Form/ModulesUninstallForm.php +++ b/core/modules/system/src/Form/ModulesUninstallForm.php @@ -144,7 +144,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { } // Sort all modules by their name. - uasort($uninstallable, 'system_sort_by_info_name'); + uasort($uninstallable, [ModuleExtensionList::class, 'sortByName']); $validation_reasons = $this->moduleInstaller->validateUninstall(array_keys($uninstallable)); $form['uninstall'] = ['#tree' => TRUE]; diff --git a/core/modules/system/system.module b/core/modules/system/system.module index bbc0e1fcbf02f809a15d31bb13d136c6f8787c78..bb91e01716bce5b894bde98057879813085d0502 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -891,16 +891,14 @@ function system_region_list($theme, $show = REGIONS_ALL) { /** * Array sorting callback; sorts modules by their name. + * + * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use + * \Drupal\Core\Extension\ExtensionList::sortByName() instead. + * + * @see https://www.drupal.org/node/3225999 */ function system_sort_modules_by_info_name($a, $b) { - @trigger_error('system_sort_modules_by_info_name() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Implement system_sort_by_info_name() instead. See https://www.drupal.org/node/3225624', E_USER_DEPRECATED); - return system_sort_by_info_name($a, $b); -} - -/** - * Array sorting callback; sorts modules by their name. - */ -function system_sort_by_info_name($a, $b) { + @trigger_error('system_sort_modules_by_info_name() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Extension\ExtensionList::sortByName() instead. See https://www.drupal.org/node/3225999', E_USER_DEPRECATED); return strcasecmp($a->info['name'], $b->info['name']); } @@ -910,7 +908,7 @@ function system_sort_by_info_name($a, $b) { * Callback for uasort() within * \Drupal\system\Controller\SystemController::themesPage(). * - * @see system_sort_by_info_name() + * @see \Drupal\Core\Extension\Extension::sortByName() */ function system_sort_themes($a, $b) { if ($a->is_default) { diff --git a/core/modules/system/tests/src/Kernel/SystemDeprecationTest.php b/core/modules/system/tests/src/Kernel/SystemDeprecationTest.php index 991c5122434d63cdd1e11f37247d463ae8ad2bf8..1c0ade988fd19a69d64ef9c8cbe2ab3f42635559 100644 --- a/core/modules/system/tests/src/Kernel/SystemDeprecationTest.php +++ b/core/modules/system/tests/src/Kernel/SystemDeprecationTest.php @@ -19,13 +19,15 @@ class SystemDeprecationTest extends KernelTestBase { * @see system_sort_modules_by_info_name() */ public function testSystemSortModulesByInfoName() { - $module_info = []; - foreach (\Drupal::service('extension.list.module')->getAllInstalledInfo() as $module => $info) { - $module_info[$module] = new \stdClass(); - $module_info[$module]->info = $info; - } - $this->expectDeprecation('system_sort_modules_by_info_name() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Implement system_sort_by_info_name() instead. See https://www.drupal.org/node/3225624'); - uasort($module_info, 'system_sort_modules_by_info_name'); + $module_info = \Drupal::service('extension.list.module')->getAllInstalledInfo(); + $to_sort = [ + 'user' => (object) ['info' => $module_info['user']], + 'system' => (object) ['info' => $module_info['system']], + ]; + + $this->expectDeprecation('system_sort_modules_by_info_name() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Extension\ExtensionList::sortByName() instead. See https://www.drupal.org/node/3225999'); + uasort($to_sort, 'system_sort_modules_by_info_name'); + $this->assertSame(['system', 'user'], array_keys($to_sort)); } }