Skip to main content
Category:

Question

How to override page title in drupal8?

Solution

use $variables['title']  in function THEMENAME_preprocess_page_title(&$variables) { } to set the page title.

Example

/**
 * @param $variables
 */
function mytheme_preprocess_page_title(&$variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    switch ($node->getType()){
      case "article":
        $variables['title'] = t('News');
        break;
      case "page":
        if ( $node->id() == "3"){
          $variables['title'] = t('Page Title');
        }
        break;
    }
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook