7

When I get the title on some pages, I get an empty title like Home. The code I am using in my subscriber is the following one.

class HistorySubscriber implements EventSubscriberInterface {
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('onResponse', 0);
    return $events;
  }

  public function onResponse($event) {
    $request = \Drupal::request();
    $route_match = \Drupal::routeMatch();
    $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
  }
}

It is an issue? How I can fix it?

4
  • 1
    There is currently no clear API to do it, sadly. There used to be some issue to discuss that problem. The information is available in either hook_preprocess_page ... or temporarily in \Drupal\Core\Render\HtmlRenderer::prepare(), which sadly does not pass the title to any event. I guess we should fill an issue against drupal core to solve the situation ... as this is a clear usecase for people Dec 29, 2014 at 14:51
  • @DanielWehner that is, let me create a issue in drupal.org
    – rpayanm
    Dec 29, 2014 at 18:03
  • Could you please post the link to the issue you've filed on drupal.org?
    – anavarre
    Jan 6, 2015 at 15:14
  • @anavarre this is the issue drupal.org/node/2399615, but was closed because it is a duplicated of drupal.org/node/2264043
    – rpayanm
    Jan 6, 2015 at 16:44

3 Answers 3

1

The following code can be used to change page title.

// Alter page title.
$request = \Drupal::request();
if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
  $route->setDefault('_title', 'Kontakt');
}
0

Try this

<?php
  $request = \Drupal::request();
  $title=t('Home');
  if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT) && !drupal_is_front_page()) {
    $title = \Drupal::service('title_resolver')->getTitle($request, $route);
  }

?>

this code return null in front page, then you should try to check if drupal_is_front_page then show another title

4
  • That code does not work, on "Home" page $title is null
    – rpayanm
    Dec 29, 2014 at 14:23
  • @RolandoPayánMosqueda if your problem is only in home, try detect front page then change it, I update my answer
    – Yuseferi
    Dec 29, 2014 at 17:51
  • Not only on "Home" occur this, on "admin/content" too and I am sure on others pages more.
    – rpayanm
    Dec 29, 2014 at 18:06
  • @RolandoPayánMosqueda I think it is a drupal issue,drupal 8 is beta3,don't expect everything work correctly,specially new features and APIs :)
    – Yuseferi
    Dec 30, 2014 at 5:46
0

Use this code inside your function and You will get title of any page like term, node page...

public function getCurrentPageTitle() {
    $title = 'DEFAULT TITLE';
    $request = \Drupal::request();
    if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
      // For Taxonomy Term
      if (!empty($request->attributes->get('taxonomy_term'))) {
        $title = $request->attributes->get('taxonomy_term')->get('title')->value;
      }
      // For Node
      else {
        $node = \Drupal::routeMatch()->getParameter('node');
        if (!empty($node) && 'CONTENT_TYPE' === $node->bundle()) {
          $title = "CONTENT_TYPE_SPECIFIC_TITLE_SET";
          //$title = \Drupal::service('title_resolver')->getTitle($request, $route);
        }
        else {
          $title = \Drupal::service('title_resolver')->getTitle($request, $route);
        }
      }
    }
    return $title;
}

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.