Adding action links in .links.action.ymlfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Creating actionable user interfaces is crucial in Drupal module development. Action links play a significant role by providing direct, contextual actions in the user interface. This lesson will guide you on adding action links to your custom Drupal module using the .links.action.yml file, enabling users to perform specific tasks efficiently within your module’s interface.

Understanding Action Links

Action links in Drupal are elements providing users with calls-to-action (CTAs) situated within specific contexts, like admin pages or user content. They are used to trigger actions such as adding new content or settings changes, streamlining user interactions and workflows.

Setting Up Action Links in Drupal

To effectively implement action links in your custom module, you’ll utilize the .links.action.yml file. Here’s how to properly configure action links for improved functionality:

Step 1: Create the .links.action.yml File

If your module does not already have one, create a .links.action.yml file in your module’s directory. This file holds the configuration for your action links.

      
          touch modules/custom/my_rest_module/my_rest_module.links.action.yml
      
    

Step 2: Define Action Links in YAML

Edit the my_rest_module.links.action.yml file to define the links. Below is an example configuration:

      
          my_rest_module.add_item:
            route_name: my_rest_module.add_item_form
            title: 'Add New Item'
            appears_on:
              - my_rest_module.overview

          my_rest_module.clear_cache:
            route_name: cache.admin
            title: 'Clear All Caches'
            appears_on:
              - system.admin_config_performance
      
    

In this setup:

  • Add New Item: Directs users to a form for adding new items, connected to the overview page of your module.
  • Clear All Caches: Provides a direct link to clear caches, appearing on the performance configuration page.

Field Descriptions

  • route_name: The internal route link destination for the action.
  • title: Text label displayed on the action link.
  • appears_on: A list of routes where this action link is visible, linking it to specific pages where the related context or functionality exists.

Defining Routes for Action Links

Each action link must align with defined routes in your module’s .routing.yml file. Here’s an example defining the necessary routes:

      
          my_rest_module.add_item_form:
            path: '/admin/my-rest-module/add-item'
            defaults:
              _form: '\Drupal\my_rest_module\Form\AddItemForm'
              _title: 'Add New Item'
            requirements:
              _permission: 'administer my rest module'

          my_rest_module.overview:
            path: '/admin/my-rest-module/overview'
            defaults:
              _controller: '\Drupal\my_rest_module\Controller\OverviewController::overview'
              _title: 'Module Overview'
            requirements:
              _permission: 'administer my rest module'
      
    

The routes define actual endpoints where the actions will take users, thus ensuring that the action links accurately guide users to the intended functionalities.

Testing Your Action Links

Once you configure your action links, they need verification. Clear Drupal’s cache to ensure the system reflects all recent changes:

      
          drush cr
      
    

Navigate to the relevant administration pages and ensure the action links appear correctly and perform as expected. Check that they lead to the appropriate routes and functionalities.

Conclusion

By integrating action links in your Drupal modules, you create a streamlined, user-friendly interface for administrators and users alike. Understanding how to configure these through .links.action.yml improves your module’s functionality and overall user experience.

What’s Next?

While defining links is essential, customizing and extending them takes your module’s capabilities to an advanced level. The next lesson will guide you through Modifying Links with hook_menu_links_discovered_alter(), providing a hands-on approach to dynamically alter and extend menu link behaviors programmatically.