How do I set the title of the tab from a controller in Drupal 8? This is the code I am using:


class myController extends ControllerBase {

    public function node($nId) {
        $nodeObj = Node::load($nId);
        $elements = getRequiredDetails($nodeObj);

        return [
            '#theme'    => 'my_mod',
            '#title'    => 'This is the Tab Title'
            '#contents' => $elements,
            '#attached' => [
                'library'=> [

    * Returns a page title.
    public function getTitle() {
        $route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();

        return $route->getDefaults('#title');


 path: '/node/{nId}'
   _controller: 'Drupal\my_mod\Controller\myController::node'
   _title_callback: 'Drupal\my_mod\Controller\myController::getTitle'
   _permission: 'access content'

But the '#title' in my controller sets the title in the page, not in the tab. Is there anything I am doing wrong?

If I put something like die('Title Here'); in getTitle(), it will show in the tab. I need to call that method from the route.

  • Which tab? I don't see any tab in the code you have provided.
    – No Sssweat
    Dec 4, 2017 at 5:26
  • I need a title to be set on the browser tab. Looks like #title doesnt work for browsers tab head. It only shows title in the contents. @NoSssweat Dec 4, 2017 at 5:31
  • 1
    Drupal doesn't control what a browser shows in its tabs. By default, it's the page title, but if there are extensions changing the tab title, Drupal cannot do anything.
    – apaderno
    Dec 4, 2017 at 7:46
  • Yes, there is a lot of code trying to set titles, Metatag for example does this too, it's pre-configured to set the page title for nodes by this tokens: [node:title] | [site:name].
    – 4uk4
    Dec 4, 2017 at 8:16
  • @kiamlaluno yes but the thing is my title does not show on the tab. It is just the Drupal Site Name. I guess the default format is PageTitle | SiteName. But my one is showing as | SiteName Dec 5, 2017 at 5:55

1 Answer 1


If you use Meta tag module, you can use following method. From this method you can get Meta tag title and other Meta tags for view section also.


class: Drupal\my_module\Routing\RouteSubscriber
  - { name: event_subscriber }


namespace Drupal\my_module\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

 * Listens to the dynamic route events.
class RouteSubscriber extends RouteSubscriberBase {

     * {@inheritdoc}
    protected function alterRoutes(RouteCollection $collection) {

        if ($route = $collection->get('entity.node.canonical')) {
                '_controller' => 'Drupal\my_module\Controller\MymoduleController::view',


namespace Drupal\my_module\Controller;

use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Controller\NodeViewController;

class MymoduleController extends NodeViewController {

    public function view(EntityInterface $node, $view_mode = 'full', 
    $langcode = NULL) {

        $elements = getRequiredDetails($node);

        return [
            '#theme'    => 'your-theme',
            '#contents' => $elements,
            '#attached' => [
                'library'=> [

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.