Change the deriver class of a menu link

Created on 21 November 2024, 5 months ago

Problem/Motivation

We need to remove some of the admin toolbar links for our project. I've attempted to override the 'admin_toolbar_tools.extra_links' deriver class, "ExtraLinks", with a custom one. One thing I tried is to define the same menu link in the menu.yml file in my custom module, changing the deriver class, but I think this is not correct, is it a better way to do it?

Thanks in advance.

💬 Support request
Status

Active

Version

10.4

Component

menu system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @clgarciab
  • 🇦🇺Australia nterbogt

    This ticket is best placed on the admin_toolbar module rather than core.

    That said, you can't currently override a deriver as far as I'm aware. The code to do that doesn't exist, see Allow base plugin definition to be altered before derived Active .

    You can however override the menu links by using a module hook. Here is an example from our codebase.

    /**
     * Implements hook_menu_links_discovered_alter().
     */
    function MY_MODULE_menu_links_discovered_alter(&$links) {
      foreach ($links as $route_name => $link) {
        // Remove all the individual views from the menu.
        if (\str_starts_with($route_name, 'admin_toolbar_tools.extra_links:views_ui') && !\in_array($route_name, ['admin_toolbar_tools.extra_links:views_ui.add', 'admin_toolbar_tools.extra_links:views_ui.field_list'])) {
          unset($links[$route_name]);
        }
      }
    }
    
  • 🇫🇷France dydave

    Thanks a lot @nterbogt for the very clear and great answer, it is very helpful! 🙏

    Allow other modules to alter/modify the menu links added by admin_toolbar (or any of its sub-modules).

    This seems to be a rather frequent/recurring request and requirement, so we should probably create a specific documentation page to explain and describe the approach suggested at #2.
    Information about other potential methods of extension of the Extralinks class and why it was made final could also be explained in the same documentation, with a specific section, see for example:

     
    Ideally, a paragraph should also be added on project's page to direct users to this ticket and the documentation page:
    "Extending Admin Toolbar" > "Overriding menu links added by Admin Toolbar Tools"
    The same changes would probably also need to be added to module's README.md file or in sub-module admin_toolbar_tools

    Re-qualifying ticket as an active Task which should hopefully result in the creation of a new documentation page for the module.

    Feel free to let us know if you have any suggestions, comments or questions on this particular comment, or the issue in general, we would surely be glad to help.
    Thanks in advance! 🙂

Production build 0.71.5 2024