Drupal - How do you display the 'Title' field on display/page for custom content type?

In D8 the title is not handled as a normal field. As example for nodes the title is displayed in two ways:

  1. Block: There is a block "Page Title". You can place this block in any region, it' possible to place it multiple times and the visibility can be configured so that it does not appear on every page.

  2. Template: If the node is not displayed as page, then the title is displayed inside the node template.

You can use the same methods for your custom entity.

The first method should work with any custom entity. If you have a route to the entity and you place the block "Page Title" on your site, then the block should be able to resolve the title of the entity.

For the second method you have to put the title in a variable in preprocess and place it in the twig file.

Edit: You have no custom entity, it is a custom content type. So the title is there already as variable "label" and can be used right away:

node--custom-content-type.html.twig

  <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>

This example is the linked title, but you can simply use {{ label }} if you don't need this.


I don't like the idea of falling back to another block (as I wanted a title field) or to needing to set the title in templates (too much logic in templates can get difficult to maintain on the long run). At least there seems to be some ongoing discussion on DO how to [META] Expose Title and other base fields in Manage Display after the loved Title module seems to be abandoned.

I also wouldn't recommend to install the pretty massive Display Suite for such a small thing. The new experimental core module Layouts Builder (video) might cover that, but still seems quite fiddly to configure.

So for now I live with the following little gem (source 1, source 2) that adds a new pseudo field to the display settings of each content type to render the page title as simple markup at the corresponding position inside the node's $content.

use Drupal\node\Entity\NodeType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

/**
 * Implements hook_entity_extra_field_info().
 */
function MYMODULE_entity_extra_field_info() {

  $extra = [];

  foreach (NodeType::loadMultiple() as $bundle) {

    $extra['node'][$bundle->id()]['display']['title_field'] = [
      'label'       => t('Title'),
      'description' => t('Title field'),
      'weight'      => 100,
      'visible'     => TRUE,
    ];
  }

  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MYMODULE_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  // You could get the node ID or anything else
  // you want from the node object here.
  $title = $entity->getTitle();

  if ($display->getComponent('title_field')) {

    $build['title_field'] = [
      '#type'     => 'inline_template',
      '#template' => '<h1>{{ title }}</h1>',
      '#context'  => [
        'title' => $title,
      ],
    ];
  }
}

Just found out there already is a little module which does exactly that: Pseudo Title.

Pseudo Title creates a pseudo field that can be used to move up and down the content of the title in the same administration page where the display of other fields are managed.

And there also is the Manage display module.

This project makes base fields such as 'title' available in "Manage Display".

This very simple module uses the framework that is already in Core rather than inventing anything new. The goal is that most or all of this module will be absorbed into core #2353867: [META] Expose Title and other base fields in Manage Display.


If you inspect the entity from the scope of your TWIG templates the title should be available there. Also Node::getTitle()

Tags:

Entities

8