Skip to main content
Category:

How to override a local task in Drupal 8

 

For Drupal 8, this is what worked for me. I also adjusted the weights so I could re-order the items.

/**
 * Implements hook_local_tasks_alter().
 *
 * @inheritdoc
*/
function mymodule_local_tasks_alter(&$local_tasks) {

  $local_tasks['entity.user.canonical']['title'] = 'Account Details';
  $local_tasks['entity.user.canonical']['weight'] = -3;

  $local_tasks['entity.user.edit_form']['title'] = 'Edit Account';
  $local_tasks['entity.user.edit_form']['weight'] = -2;

}

 

Example 2:

/* mycomment.module */

/**
 * Set the unapproved comments count.
 *
 * Implements hook_menu_local_tasks_alter().
 */
function mycomment_menu_local_tasks_alter(&$data, $route_name, &$cacheability) {
  if ($route_name == 'comment.admin' ||
    $route_name == 'comment.admin_approval' ||
    $route_name == 'mycomment.admin_comment_spam'
   ) {
    $data['tabs'][1]['comment.admin_approval']['#link']['title'] =
      t('Unapproved comments (@count)', [
        '@count' => get_actual_unapproved_comments(),
    ]);
  }
}

 

/* mycomment.module */

/**
 * Get the number of unapproved comments.
 *
 * @return int
 *   The number of unapproved comments excluding the spam comments.
 */
function get_actual_unapproved_comments() {
  $query = \Drupal::entityQuery('comment');
  $query->condition('status', 0);
  $orCondition = $query->orConditionGroup();
  $orCondition->condition('field_spam', NULL, 'IS NULL');
  $orCondition->condition('field_spam', FALSE, '=');
  $query->condition($orCondition);
  $ids = $query->execute();
  return count($ids);
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook