Similar contents

Tag: Create or change menu local tasks titles in Drupal

Create or change menu local tasks titles in Drupal

Using Hook or OOP you can customize the title of these menu local tasks tabs or add a new one

By

pablito

created on Friday, January 12, 2024 - 02:38AM - UTC

Content body

I don't know about you, but I found some titles too big or in some cases I want to show different titles, and so I looked for alternatives to achieve that and I found this way.

To contenxt, I got these two examples of tab menu, the first one is from
login form (Log in, Create new account, Password Recovery).

Drupal Menu Local Tasks Customization - Password Recovery Menu


and this one is from administrative pages (Content, Comments and Files).

Drupal Menu Local Tasks Customization - Content tabs

 


Creating new tab item on menu local tasks using Hook

The first method is through hook_menu_local_tasks_alter, you basically should know the route name of the tab. This user.pass is the tab from password recovery. As you can see, you can add some static title or get dynamically as well.  

<?php
 
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
 
/**
* Implements hook_menu_local_tasks_alter().
*/
function custom_module_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
 
  if ($route_name === 'user.pass') {
    $data['tabs'][0]['user.pass']['#link']['title'] = 'add your code or title here';
  }
 
}


On your custom module root, create the routing file: custom_module.routing.yml
In this example I'm using an Controller:

google.login:
  path: '/user/login/glogin'
  defaults:
    _controller: '\Drupal\custom_module\Controller\GoogleTab::googleTabItem'
    _title: 'Google Link'
  requirements:
    _permission: 'access content'


Also on you module root, create the links tasks file: custom_module.links.task.yml, here the key point is to know which base_route you want to add your new item to, in this case are: user.page

google.login:
  route_name: google.login
  base_route: user.page
  title: 'Google Login'
  options:
    attributes:
      class: ['google-tab-link']


And finally the Controller, really simple controller that redirect the user to /google.
You are free to build what you want here, enjoy!

<?php
 
namespace Drupal\custom_module\Controller;
 
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
 
/**
* Controller for the custom tab.
*/
class GoogleTab extends ControllerBase {
 
 /**
  * Callback for the custom account link.
  */
  public function googleTabItem() {
    // Your custom content goes here.
    return new RedirectResponse('google');
  }
 
}

 

 

Creating new tab item on menu local tasks OOP

On you module root, create the links tasks file: custom_module.links.task.yml, here the key point is to know which base_route you want to add your new item to, in this case are: user.page

google.login:
  route_name: google.login
  base_route: user.page
  title: Google link
  class: '\Drupal\custom_module\Plugin\Menu\LocalTask\GoogleTab'


On path: custom_module/src/Plugin/Menu/LocalTask creates you class, in this case: GoogleTab.php
Make sure that you class extends from LocalTaskDefault and now you can override the title as you wish.

<?php

namespace Drupal\custom_module\Plugin\Menu\LocalTask;
 
use Drupal\Core\Menu\LocalTaskDefault;
use Symfony\Component\HttpFoundation\Request;

class GoogleTab extends LocalTaskDefault {
  /**
   * {@inheritdoc}
   */
  public function getTitle(Request $request = NULL) {
    return 'add your code or title here';
  }

}

That's it!!

More content for you

Join the conversation

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
user:
Texugo
email:
pablorigueto@hotmail.com
Texugo
369 days

Tks!

user:
pablito
email:
pabloedurigueto@outlook.com
pablito
369 days

In reply to by Texugo

@Texugo:

:)

keyboard_arrow_up