drupal 8 add tab to custom controller
So this particular situation is a simple one, and yet all of the answers on Google were very specific to adding another tab to a location that already has tabs. None discussed how to start adding tabs to a page (such as a custom route and controller) where there are no tabs currently.
I'll ruin the surprise by just saying the trick is you need a tab (local task) for the page you are on, as well as the tab you wish to make available to users. Kind of like how Node has "view" and "edit" tabs, you need to essentially create that "view" tab. Without a tab for your current route, it won't care about the other local tasks you've created for that route.
Some example code:
my_module.links.task.yml
my_module.view_profile:
title: 'View Profile'
base_route: 'my_module.people'
route_name: 'my_module.people'
weight: 3
my_module.edit_profile:
title: 'Edit Profile'
base_route: 'my_module.people'
route_name: 'my_module.edit_profile'
weight: 3
my_module.routing.yml
my_module.people:
path: '/people/{id}'
defaults:
_controller: '\Drupal\my_module\Controller\PeopleProfile::renderablePeopleProfile'
_title_callback: '\Drupal\my_module\Controller\PeopleProfile::getTitle'
requirements:
_permission: 'access content'
_custom_access: '\Drupal\my_module\Controller\PeopleProfile::access'
my_module.edit_profile:
path: '/people/{id}/edit'
defaults:
_controller: '\Drupal\my_module\Controller\ProfilesEditTab::editProfileForward'
requirements:
_permission: 'access content'
The key thing to note is in links.task.yml
See how we have the route for people that works with an ID variable? We want on that page to display the edit tab. Welp, you can't just do the edit tab, you also need the tab for the current page you are on. That's where having the tab (links.task.yml) with the base_route and route_name matching the very place you want tabs at all is essential. Without that, Drupal just won't render tabs at all.