How can I change page title in Drupal 8?

There are a couple of solutions to change the page title


/**
 * Implements THEME_preprocess_page_title()
 */
function THEMENAME_preprocess_page_title(&$variables) {
  $current_url = \Drupal\Core\Url::fromRoute('<current>');
  $url = $current_url->getInternalPath();
  if($url == 'home') {  
    $variables['title'] = 'Welcome to Development Portal | ThirstySix';
  }
}

/** 
 * Implements THEME_preprocess_page(). 
 */
function THEMENAME_preprocess_page(&$variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if ($node) {
    $variables['title'] = $node->getTitle();
  }
}

 

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function MODULENAME_user_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  if ($entity->getEntityTypeId() == 'user') {
    $build['#title'] = $entity->get('field_display_name')->getString();
  }
}
Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
10 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Instagram Without API

Could not reach Instagram server