By Filipe Miguel Fonseca


2016-01-25 09:28:03 8 Comments

I'm using the pagetitle core module to render a page title. However, for certain content types I want the page main title to be the content type label (e.g. "News") and not the node label (e.g. "Drupal 8 released!").

Is there a simple way to achieve it? My first guess was to use the template_preprocess_page_title but $variables has no context whatsoever about node, node type, etc...

5 comments

@Zyfraglover 2016-07-19 12:47:26

Linus got it right for the preprocess function, but I'd personally use this to get the current node (as it's shorter and seem easier...)

$node = \Drupal::request()->attributes->get('node')

Then to access the node title, use :

$title = $node->getTitle()

or to access another custom field (with a value) :

$node->get('field_MYFIELD')->value

Actually, access everything the way you'd access these datas in a regular node preprocess function :)

@Francis 2018-09-24 03:17:23

In THEMENAME_preprocess_html(&$variables) {} there is the $variables['head_title'] which is the highest hand - instance for the page title (Structured title: $variables['head_title_array'] ). There are also other interesting array keys like $variables['page']['content'] and $variables['node_type'] based on which the title can be processed.

Final thought: $variables['head_title'] = ['title_part1','title_part2']; since in the html.html.twig

<title>{{ head_title|safe_join(' | ') }}</title>

@Joe Hyde 2016-12-28 22:07:15

Make a small module. Two files required:

File name: liveeventtitles.info.yml

name: Live Event Titles
description: Programmatically generates titles for event node bundles.
package: Custom
type: module
version: 1.0
core: 8.x
dependencies:
  - node

File: liveeventtitles.module

<?php
/**
 * @file
 * Modify the node create/edit form and automatically generate a node title for event nodes.
 */
define('LIVEEVENTTITLES_PLACEHOLDER', '%LiveEventTitle%');
/**
 * Implements hook_form_alter().
 */
function liveeventtitles_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_event_form' || $form_id == 'node_event_edit_form') {
    $title_widget = &$form['title']['widget'][0];
    $default = (!empty($title_widget['value']['#default_value'])? $title_widget['value']['#default_value'] : LIVEEVENTTITLES_PLACEHOLDER);
    $title_widget['value']['#default_value'] = $default;
    $title_widget['value']['#type'] = 'value';
    $title_widget['value']['#required'] = FALSE;
    $form['title']['#access'] = FALSE;
  }
}
/**
 * Implements hook_node_presave
 */
function liveeventtitles_node_presave(Drupal\node\Entity\Node $node) {
  $type = $node->getType();
  if ($type == 'event') {
    // Load the artist node to get the title
    if ($artist_id = $node->field_artist->getString()) {
      $artist = \Drupal\node\Entity\Node::load($artist_id);
      $artist_name = $artist->title->getString();
    }
    // Load the Venue to get the title
    if ($venue_id = $node->field_venue->getString()) {
      $venue = \Drupal\node\Entity\Node::load($venue_id);
      $venue_name = $venue->title->getString();
    }
    if (!empty($venue_name) && !empty($artist_name)) {
      $node->setTitle($artist_name . ' at ' . $venue_name);
    }
  }
}

I probably have more than I need, such as I don't think I need a default value and PLACEHOLDER in the hook_form_alter() but I need to give someone a reason to vote this down (-;

@Linus 2016-01-25 19:02:50

As mentioned by Ivan Jaros You can use hook_preprocess_page_title.

You just have to load the node from the route first, to get some context.

function yourtheme_preprocess_page_title(&$variables) {

  // Load the node entity from current route
  if ($node = \Drupal::routeMatch()->getParameter('node')) {

    // Load the label of the bundle
    $bundle_label = \Drupal::entityTypeManager()
      ->getStorage('node_type')
      ->load($node->bundle())
      ->label();

    // Set the page title
    $variables['title'] = $bundle_label;
  }
}

If you just want to use this with certain content types, you can use $node->bundle() to get the machine-readable name and check against it.

@Malabya Tewari 2016-10-15 10:10:31

This changes the title for the block_title. However it doesn't override the Page title. Any idea how to get the page title set to same value as the title block

@user21641 2016-01-25 09:50:16

Use the Metatag module it is D8-ready already and supports what you need.

@user21641 2016-01-25 09:51:24

Although I am not 100% if it supports overriding title in the page title block. At this point I only know it works for page title.

@Filipe Miguel Fonseca 2016-01-25 11:53:50

Thanks for the input Ivan Jaros, as far as I can tell the supported page title is the one on the HTML head, I want to override the main title.

@user21641 2016-01-25 13:06:02

In that case hook_preprocess_page_title. Load the node from route, get the entitytype and get label from there.

@Kevin 2018-04-18 21:00:09

This worked for me for the user profile page.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How tu setup magazine-like content type?

  • 2018-11-19 20:16:55
  • Dariusz ChowaƄski
  • 45 View
  • -2 Score
  • 1 Answer
  • Tags:   8 nodes

4 Answered Questions

[SOLVED] How do you display the 'Title' field on display/page for custom content type?

  • 2016-08-05 21:38:36
  • kvjava1
  • 5741 View
  • 3 Score
  • 4 Answer
  • Tags:   8 entities

1 Answered Questions

[SOLVED] Check Content Type in Page PreProcess Hook

  • 2018-06-19 14:13:38
  • user7355700
  • 617 View
  • 1 Score
  • 1 Answer
  • Tags:   8 theming

0 Answered Questions

Twig template for field of type Content

  • 2018-05-31 12:51:02
  • Tibo
  • 26 View
  • 0 Score
  • 0 Answer
  • Tags:   8 theming images

2 Answered Questions

[SOLVED] Hide page title if the image field has a value

  • 2018-04-05 13:36:58
  • MrPeanut
  • 203 View
  • 1 Score
  • 2 Answer
  • Tags:   8 theming

1 Answered Questions

1 Answered Questions

[SOLVED] Cannot add 'Page Title' and 'Main Content' blocks to panel

  • 2016-07-07 15:14:28
  • cornuz
  • 554 View
  • 0 Score
  • 1 Answer
  • Tags:   8 panels

1 Answered Questions

[SOLVED] Render value of custom content type in html.html.twig

  • 2016-11-24 16:31:44
  • benandunt
  • 413 View
  • 2 Score
  • 1 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] How can I alter the title of a node upon save?

  • 2016-01-31 03:49:39
  • Patrick Kenny
  • 388 View
  • 0 Score
  • 2 Answer
  • Tags:   nodes hooks 8

0 Answered Questions

How to override 'H1 page title' and how to group content types?

  • 2015-11-17 08:49:50
  • Big B.
  • 48 View
  • 1 Score
  • 0 Answer
  • Tags:   forms theming

Sponsored Content