Problem/Motivation

Part of #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() effort. Remove usage of drupal_static() from system_get_module_admin_tasks().

Proposed resolution

  • Add two new services:
    • user.module_permissions_link_helper => Drupal\user\ModulePermissionsLinkHelper for module permission links and
    • system.module_admin_links_helper => Drupal\system\ModuleAdminLinksHelper for module admin links
  • Deprecate system_get_module_admin_tasks().
  • Deprecate usage of drupal_static_reset('system_get_module_admin_tasks').

Remaining tasks

  • Add tests for the new services
  • Consider renaming to \Drupal\system\ModuleAdminLinkHelper and \Drupal\user\ModulePermissionsLinkHelper
  • Consider using \Drupal\Core\Cache\MemoryCache\MemoryCache to keep the ability to flush the cache externally

User interface changes

None.

API changes

  • Added two new services:
    • user.module_permissions_link_helper => Drupal\user\ModulePermissionsLinkHelper for module permission links and
    • system.module_admin_links_helper => Drupal\system\ModuleAdminLinksHelper for module admin links
  • system_get_module_admin_tasks() is deprecated.
  • Using drupal_static_reset() with system_get_module_admin_tasks as parameter is deprecated.

Data model changes

None.

Release notes snippet

N/A

Issue fork drupal-3038971

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Status: Active » Needs review
StatusFileSize
new15.07 KB

patch.

claudiu.cristea’s picture

Title: Move system_get_module_admin_tasks() into a helper service and deprecate it » Move system_get_module_admin_tasks() into a trait and deprecate it
Issue summary: View changes
StatusFileSize
new20.62 KB
new13.41 KB

In fact this should be a trait, not a service, as is only code reusing between AdminController and HelpController.

andypost’s picture

Mixed feelings about trait
it does 3 things
- transforms menu links a-ka http://cgit.drupalcode.org/drupal/tree/core/modules/system/src/Plugin/Bl...
- check access to links
- build links

I bet it fits both controllers and blocks

Maybe makes sense to move it to render element?

$build['menu_tree'] =[
  '#type' => 'menu_tree',
  '#extension' => 'help',
  '#transform' => [], // could define tree transformations additionally to defaults or override
];
  1. +++ b/core/modules/help/src/Controller/HelpController.php
    @@ -136,7 +139,7 @@ public function helpPage($name) {
           // Only print list of administration pages if the module in question has
           // any such pages associated with it.
    -      $admin_tasks = system_get_module_admin_tasks($name, system_get_info('module', $name));
    +      $admin_tasks = static::getModuleAdminTasks($name);
           if (!empty($admin_tasks)) {
             $links = [];
             foreach ($admin_tasks as $task) {
    
    +++ b/core/modules/system/src/Controller/AdminController.php
    @@ -27,7 +30,7 @@ public function index() {
         foreach ($module_info as $module => $info) {
           // Only display a section if there are any available tasks.
    -      if ($admin_tasks = system_get_module_admin_tasks($module, $info->info)) {
    +      if ($admin_tasks = static::getModuleAdminTasks($module)) {
             // Sort links by title.
             uasort($admin_tasks, ['\Drupal\Component\Utility\SortArray', 'sortByTitleElement']);
    

    similar controllers to render links finally

  2. +++ b/core/modules/system/src/AdminHelperTrait.php
    @@ -0,0 +1,93 @@
    +trait AdminHelperTrait {
    ...
    +      $parameters = new MenuTreeParameters();
    +      $parameters->setRoot('system.admin')->excludeRoot()->onlyEnabledLinks();
    +      static::$menuTree = $menu_link_tree->load('system.admin', $parameters);
    +      $manipulators = [
    +        ['callable' => 'menu.default_tree_manipulators:checkAccess'],
    +        ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    +        ['callable' => 'menu.default_tree_manipulators:flatten'],
    +      ];
    +      static::$menuTree = $menu_link_tree->transform(static::$menuTree, $manipulators);
    

    This is tree transformation, which waiting for alter in system menu block, issue #2854013: Allow SystemMenuBlock tree manipulators to be altered

  3. +++ b/core/modules/system/src/AdminHelperTrait.php
    @@ -0,0 +1,93 @@
    +    foreach (static::$menuTree as $element) {
    +      if (!$element->access->isAllowed()) {
    ...
    +      if ($link->getProvider() !== $module) {
    ...
    +      $admin_tasks[] = [
    +        'title' => $link->getTitle(),
    +        'description' => $link->getDescription(),
    +        'url' => $link->getUrlObject(),
    

    actually render element filtered by module(extension?)

  4. +++ b/core/modules/system/src/AdminHelperTrait.php
    @@ -0,0 +1,93 @@
    +    /** @var \Drupal\user\PermissionHandlerInterface $permission_handler */
    +    $permission_handler = \Drupal::service('user.permissions');
    ...
    +    if ($permission_handler->moduleProvidesPermissions($module)) {
    +      /** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
    +      $access_manager = \Drupal::service('access_manager');
    +      if ($access_manager->checkNamedRoute('user.admin_permissions', [], \Drupal::currentUser())) {
    +        $url = new Url('user.admin_permissions');
    +        $url->setOption('fragment', 'module-' . $module);
    +        $admin_tasks["user.admin_permissions.$module"] = [
    +          'title' => t('Configure @module permissions', ['@module' => \Drupal::moduleHandler()->getName($module)]),
    +          'description' => '',
    +          'url' => $url,
    

    user namespace?! sounds should live in core

claudiu.cristea’s picture

@andypost, in #2 I tried to make it a helper service in system.module. The test failure is because the dependency on user module services.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

hardik_patel_12’s picture

StatusFileSize
new13.6 KB

Re-rolling for 9.1.x-dev.

Status: Needs review » Needs work

The last submitted patch, 8: 3038971-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Fixed deprecation messages (looks it needs better wording - usage will throw exception?)
And fixed tests + clean-up

Looking through changes I still not sure we can make it trait without injecting so many services

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/system/src/AdminHelperTrait.php
    @@ -0,0 +1,93 @@
    +   *
    +   * @return array
    +   *   An array of task links.
    +   */
    +  public static function getModuleAdminTasks($module) {
    +    if (!isset(static::$menuTree)) {
    +      $menu_link_tree = \Drupal::menuTree();
    +      $parameters = new MenuTreeParameters();
    +      $parameters->setRoot('system.admin')->excludeRoot()->onlyEnabledLinks();
    +      static::$menuTree = $menu_link_tree->load('system.admin', $parameters);
    +      $manipulators = [
    

    Do we really need to make it a trait if it's static? Or it could just be be a separate, standalone service. Then we can properly inject things and call it from different places. And it can be a non-static property and installing modules will automatically invalidate it.

  2. +++ b/core/modules/system/tests/src/Unit/SystemAdminModuleTasksTest.php
    @@ -0,0 +1,52 @@
    +   * @expectedDeprecation Using drupal_static_reset() with 'system_get_module_admin_tasks' is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\system\AdminHelperTrait::resetModuleAdminTasksCache() instead. See https://www.drupal.org/node/3038972
    +   * @see drupal_static_reset()
    +   */
    +  public function testSystemGetModuleAdminTasksResetCacheDeprecation() {
    +    require_once $this->root . '/core/includes/bootstrap.inc';
    +    drupal_static_reset('system_get_module_admin_tasks');
    +  }
    

    I think we don't do require_once of include files in unit tests, so this probably needs to be a kernel test. You can mock stuff there too if necessary.

and deprecation message update of course. Feels like we should have way to automate that ;)

claudiu.cristea’s picture

Title: Move system_get_module_admin_tasks() into a trait and deprecate it » Move system_get_module_admin_tasks() into a service and deprecate it
Issue summary: View changes
Status: Needs work » Needs review

I like the idea from #13, mostly because the cache reset is no more necessary. Or should we keep a "reset internal cache" method?

andypost’s picture

There's MR

claudiu.cristea’s picture

@Berdir, the problem with moving to a service with proper injection is that the user.permissions service introduces a system.module dependency to user module. This makes the test fail. Moving the service to User module would fix the problem but part of this future service still belongs to System as functionality, while the links to permissions feels like they belong to User. In a perfect world we should build the admin tasks somehow as a composition, each module (system & user) would participate to build the list of tasks. Then all dependecies are in the right place. But would it be worth it to introduce such a complexity?

EDIT: I see now 3 alternatives to the "perfect solution":

  1. Move new the service to User module (close our eyes to System functionality still inside the service).
  2. Keep the service in System but don't inject user.permissions. Create a ::getUserPermissions() protected (or private?) method that checks ::hasService() first and then returns \Drupal::service('user.permissions'). This would be a soft dependency on User module.
  3. Switch back to trait.
claudiu.cristea’s picture

Last change implements #17.2, which seem to me the most practical.

claudiu.cristea’s picture

In https://www.drupal.org/project/drupal/issues/3038971#mr928-note33503, I tried the complex solution. Maybe complex but more clean.

berdir’s picture

Fine with whatever is the most practical solution. user and system are both required modules, they're full of interdependencies, that's really nothing new and only an issue during initial installation bootstrapping and tests.

Wouldn't it be sufficient use an optionally injected dependency for those?

daffie’s picture

Status: Needs review » Needs work

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

MR needs rebase to 9.5.x

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new21.54 KB

Reroll of MR !928. Used a patch as I don't have permission to rebase it on 11.x, and creating a new MR and losing the code comments is probably more confusing for reviewers.

I replaced usage of ModuleHandler::getName() as there is an issue to deprecate that in favour of ModuleExtensionList::getName().

The existing deprecation for passing a string instead of an array to system_get_module_admin_tasks() is no longer needed as we are deprecating the whole function. Removed this and the corresponding deprecation test.

kim.pepper’s picture

StatusFileSize
new21.07 KB
new1.42 KB

Forgot to replace a usage of system_get_module_admin_tasks() and removed an unrelated change.

kim.pepper’s picture

Issue summary: View changes

Remaining tasks:

  • Add tests for the new services
  • Consider renaming to \Drupal\system\ModuleAdminLinkHelper and \Drupal\user\ModulePermissionsLinkHelper
  • Consider using \Drupal\Core\Cache\MemoryCache\MemoryCache to keep the ability to flush the cache externally

Status: Needs review » Needs work

The last submitted patch, 29: 3038971-29.patch, failed testing. View results

kim.pepper’s picture

Test is failing because link change from /admin/people/permissions/module/locale to /admin/people/permissions#module-locale. Seems like a valid fail?

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new21.09 KB
new3.43 KB

Looks like this was older than the module specific permissions page?? https://www.drupal.org/node/3223123

The last submitted patch, 28: 3038971-28.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 33: 3038971-33.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new21.11 KB
new2.11 KB

Fix missing $name arg to getModulePermissionsLink()

kim.pepper’s picture

Issue summary: View changes
StatusFileSize
new24.97 KB
new18.35 KB
  • Renamed to \Drupal\system\ModuleAdminLinkHelper and \Drupal\user\ModulePermissionsLinkHelper
  • Wrote kernel tests for the new services
kim.pepper’s picture

Remaining task:

  • Use \Drupal\Core\Cache\MemoryCache\MemoryCache to keep the ability to flush the cache externally
kim.pepper’s picture

Issue summary: View changes
kim.pepper’s picture

Issue summary: View changes
Issue tags: +Needs issue summary update, +Needs change record updates
StatusFileSize
new25.56 KB
new3.3 KB

Fixes:

  • Use \Drupal\Core\Cache\MemoryCache\MemoryCache to keep the ability to flush the cache externally
kim.pepper’s picture

Updated the IS and CR

andypost’s picture

+++ b/core/modules/user/src/ModulePermissionsLinkHelper.php
@@ -0,0 +1,61 @@
+      if ($this->accessManager->checkNamedRoute('user.admin_permissions.module', ['modules' => $module])) {
+        $url = new Url('user.admin_permissions.module', ['modules' => $module]);

this is the only reason to keep it separate module/service (usage of user's module route)

But I think it could be implemented in system module's service just checking the route presence

andypost’s picture

One more typo

+++ b/core/modules/help/tests/src/Functional/HelpTest.php
@@ -137,8 +137,11 @@ protected function verifyHelp($response = 200) {
-        $admin_tasks = system_get_module_admin_tasks($module, $info['name']);
+        $admin_tasks = \Drupal::service('system.module_admin_links_helper')->getModuleAdminLinks($module);
+        if ($module_permissions_link = \Drupal::service('user.module_permissions_link_helper')->getModulePermissionsLink($module, $name)) {

it should use $info['name'] instead of $name

kim.pepper’s picture

#42

But I think it could be implemented in system module's service just checking the route presence

Ah ok. Not sure what service provides that?

kim.pepper’s picture

StatusFileSize
new25.53 KB
new1.22 KB

Re: #43 Fixed.

Re: #42 We are using \Drupal\user\PermissionHandlerInterface::moduleProvidesPermissions() here. I'm reluctant to add another circular dependency between core, system, and user modules.

Status: Needs review » Needs work

The last submitted patch, 45: 3038971-45.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review

Random fail

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Reviewing #45

Deprecation appears to be up to date
Good test coverage
Reviewed the change record, the example was very useful and imagine will help those update later.

LGTM.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 45: 3038971-45.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new25.69 KB
new1.17 KB

Fix missing service aliases.

kim.pepper’s picture

Status: Needs review » Reviewed & tested by the community

I think it safe to put this back to RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 50: 3038971-50.patch, failed testing. View results

andypost’s picture

Status: Needs work » Reviewed & tested by the community

random failure

  • catch committed 60b67381 on 11.x
    Issue #3038971 by claudiu.cristea, kim.pepper, andypost, Berdir, daffie...
catch’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/modules/system/src/Controller/AdminController.php
@@ -18,14 +20,42 @@ class AdminController extends ControllerBase {
+
+  /**
+   * The module permissions link service.
+   *
+   * @var \Drupal\user\ModulePermissionsLinkHelper
+   */
+  protected ModulePermissionsLinkHelper $modulePermissionsLinks;
+

This shows up a general issue that we have dependencies on user module in system module, and dependencies on system module in.. a lot of things but probably also including user.

It might make sense at some point to move the admin ui building out of system module into a system_ui module that's just responsible for building admin pages. That module could then depend on system and user without issue.

There's no workaround though that I can think of except for making that third module, so committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

quietone’s picture

Published change record.