In earlier versions of Drupal, there were categories of users which showed up as tabs on the user (profile) edit page, located at /user/{user}/edit. How can we define new tabs on this page in Drupal 8?
1 Answer
Tabs can be added to the user edit page as follows:
1) Define the route(s) for your tab(s) in [MODULE].routing.yml as normal:
[MODULE].something.user_defaults:
path: '/user/{user}/edit/something'
defaults:
_controller: '\Drupal\[MODULE]\Controller\SomeController::someSettingsTab'
_title: 'Some Settings'
requirements:
_permission: 'some permission'
user: \d+
options:
parameters:
user:
type: entity:user
2) In [MODULE].links.task.yml, add your tab(s) as you would primary tabs, except rather than using base_route
, use parent_id
, and set it to (somewhat unintuitively) the user edit form tab key, which is defined in user.links.task.yml
as entity.user.edit_form
(same as the route name):
[MOUDLE].something.user_defaults:
route_name: [MODULE].something.user_defaults
parent_id: entity.user.edit_form
title: Some Title
weight: 10
3) You also need to define a tab for the user to switch back to, as tabs are only rendered if there are more than two tabs. In this case, we want to add a tab for the user edit form, using the user edit form route, again using the user edit form tab key:
[MODULE].entity.user.edit_form:
route_name: entity.user.edit_form
parent_id: entity.user.edit_form
title: Profile
-
1Step 2 here says
module.links.tasks.yml
but I think thetasks
should be singular:module.links.task.yml
. I don't see anytasks.yml
files in core/contrib.– gregglesJan 13, 2020 at 15:34 -