首页 > 解决方案 > Create custom menu tab on a users profile page in Drupal 8

问题描述

I am a Drupal newbie. I have written a custom module in Drupal 8 and it works and displays some content.

I want to add a tab in the user's profile that links to my custom module.

As far as I understand, I need to write a route and a task. This is what I have done :

my_module.routing.yml

my_module.some_route_name:
  path: '/user/{user}/some_path'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::content'
    _title: 'Some Title'
  requirements:
    _permission: 'access content'
  options:
    user: \d+

my_module.links.tasks.yml

my_module.some_task_name:
  route_name: my_module.some_route_name
  base_route: entity.user.canonical
  title: 'Some Title'

I have done this and the route works as it should, however no tab shows up in my user's profile page.

It should look something like this :

enter image description here

Edit 1

I have partially resolved the issue by creating a hook as per Drupal API 8.6 (menu_local_tasks_alter) and this is what I wrote :

function my_module_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {

$url = Drupal\Core\Url::fromRoute('my_module.some_route_name');

if ($route_name == 'entity.user.canonical') {
$data['tabs'][0]['my_module.some_route_name'] = [
    '#theme' => 'menu_local_task',
    '#link' => [
    'title' => t('Some Title'),
    'url' => $url,
    'localized_options' => [
        'attributes' => [
        'title' => t('Add content'),
        ],
    ],
    ],
];

// The tab we're adding is dependent on a user's access to add content.
$cacheability
    ->addCacheTags([
    'user.permissions',
]);
}

}

This works if I replace the {user} part of my route's path with 1. When I have {user} in the path, it now complains with the following error :

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\Routing\Exception\MissingMandatoryParametersException</em>: Some mandatory parameters are missing (&quot;user&quot;) to generate a URL for route ...

Edit 2

I have also made sure to provide my content method in my controller with a user object $user that implements AccountInterface as described here: https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes , however, I still get the same error message.

标签: drupaldrupal-modulesdrupal-8

解决方案


You don't need the hook, I just tested with your routing and _task definitions and it works fine for me.

This is my controller:

class MyModuleController {

  public function content($user) {
    return [
      '#markup' => "user: $user"
    ];
  }

}

This renders as: "user: 1" at the "/user/1/some_path" URL.

I think you are also trying to use Drupal's automatic paramater upcasting, but that is optional.

MissingMandatoryParametersException

To solve the exception (Symfony\Component\Routing\Exception\MissingMandatoryParametersException). You have to add the mandatory parameter in your route. Just add the user (['user' => 1]):

$url = Drupal\Core\Url::fromRoute('my_module.some_route_name', ['user' => 1]);

推荐阅读