1

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

Controller

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'=> [
                    'my_mod/my_mod',
                ],
            ],
        ];
    }

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

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

Route

my_mod.node:
 path: '/node/{nId}'
 defaults:
   _controller: 'Drupal\my_mod\Controller\myController::node'
   _title_callback: 'Drupal\my_mod\Controller\myController::getTitle'
requirements:
   _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 '17 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 – i am batman Dec 4 '17 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. – kiamlaluno Dec 4 '17 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]. – 4k4 Dec 4 '17 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 – i am batman Dec 5 '17 at 5:55
0

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.

my_module.services.yml

services:
my_module.route_subscriber:
class: Drupal\my_module\Routing\RouteSubscriber
tags:
  - { name: event_subscriber }

my_module/src/Routing/RouteSubscriber.php

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')) {
            $route->setDefaults(array(
                '_controller' => 'Drupal\my_module\Controller\MymoduleController::view',
            ));
        }
    }
}

MymoduleController.php

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'=> [
                    'my_content/my_content',
                ],
            ],
        ];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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