By Yusef Mohamadi


2019-03-15 09:33:41 8 Comments

I know I can customize the template by overriding page-title.html.twig but I'm looking for a way to have more than one title template. in my case, the suggested name form my path is page__manage__customer_list So I tried to override it with page--manage--customer-list-title.html.twig. how is that possible to override the title template?

to make the question more generalize and could be useful for other guys that looking for similar circumstances this could be helpful.

How can I override the title template based on content type or route?

2 comments

@Yusef Mohamadi 2019-03-15 13:33:39

Many thanks for @No Sssweat give a clue to me. her solution works for content types only. but for Taxonomy page and General pages ( custom pages you've created by custom route like me) the following snippet will work charmingly:

on YOURTHEMENAME.theme file

use \Drupal\taxonomy\Entity\Term;
/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function YOURTHEMENAME_theme_suggestions_page_title_alter(array &$suggestions, array $variables) {

  $path_args = explode('/', trim(\Drupal::service('path.current')->getPath(), '/'));
  $suggestions = theme_get_suggestions($path_args, 'page_title');

  if ($tid = \Drupal::routeMatch()->getRawParameter('taxonomy_term')) {
    $term = Term::load($tid);
    $vid = $term->vid->getValue();
    $suggestions[] = 'page_title__taxonomy__vocabulary__' . $vid[0]['target_id'];
  }

  elseif ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'page_title__' . $content_type;
  }
 // ksm($suggestions);
}

then it works for all pages.

page-title--CONTENT_TYPE_MACHINE_NAME.html.twig (sample for article content type : page-title--article.html.twig

page_title__taxonomy__vocabulary__VOCABULARY_MACHINE_NAME.html.twig ( sample for tags vocabulary is : page_title__taxonomy__vocabulary__tags.html.twig

Ref : hook_theme_suggestions_HOOK_alter

@No Sssweat 2019-03-15 11:09:27

You can add new templates to already existing using hook_theme_suggestions_HOOK_alter

Example of adding a content type:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function THEMENAME_theme_suggestions_page_title_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'page_title__' . $content_type;
  }
}

Then use page-title--content-type.html.twig


You could use $node->id(); if content type is too broad.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How to get the front page title in page.html.twig?

  • 2016-03-07 09:20:24
  • Florian Brucker
  • 2952 View
  • 2 Score
  • 2 Answer
  • Tags:   8

1 Answered Questions

How to customize profile2 module profile edit form using template method?

  • 2014-11-13 21:22:09
  • Allen Chen
  • 766 View
  • 1 Score
  • 1 Answer
  • Tags:   7 theming

1 Answered Questions

How can override the title markup on a node page?

  • 2018-11-02 00:07:31
  • dibs
  • 35 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming

0 Answered Questions

Override the title template only for specific content type page

  • 2018-07-14 09:20:03
  • user2137454
  • 15 View
  • 0 Score
  • 0 Answer
  • Tags:   8 theming

1 Answered Questions

[SOLVED] Drupal won't load properly named custom twig template

  • 2018-02-20 16:20:25
  • MathiasH
  • 23 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8 theming

0 Answered Questions

<title> blank after installing Metatag

0 Answered Questions

Views Page Display not printing title

  • 2016-09-11 13:38:40
  • Screenack
  • 558 View
  • 1 Score
  • 0 Answer
  • Tags:   views theming 8

4 Answered Questions

[SOLVED] Considerations displaying page titles in page or node template

  • 2011-08-02 21:04:03
  • Joel
  • 910 View
  • 1 Score
  • 4 Answer
  • Tags:   theming

1 Answered Questions

[SOLVED] Drupal custom page template based on the title of the page

  • 2015-04-25 11:12:53
  • Mostafa Talebi
  • 761 View
  • 2 Score
  • 1 Answer
  • Tags:   theming

1 Answered Questions

[SOLVED] overriding template file of display suite layout applies on other pages

Sponsored Content