n Drupal 8: How to override page title | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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