7

I created a content type called 'News' and when creating a 'News' entity, I am able to enter a value for the 'Title' field.

However, when I go to 'Manage display' for the content type, the 'Title' field is not listed as one of the fields that can be managed.

Looking at the page/node for a 'News' entity, the 'Title' field only appears in the breadcrumb. However, I would like to display the 'Title' field elsewhere on the page (not only in the breadcrumb).

Is there a way to achieve this?

0

5 Answers 5

8

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.

0
5

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.

2

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

0
2

There is a third way: If you have the modules Token and Token filter installed, you can use the token [current-page:title] in every place of your node and you get the rendered title. You can also put it in the content type as a default value in some field and hide the field from the user who provides the content. Then it is automatically displayed in this field.

1
  • I suggest a combination with Field Token Value to add a custom field with a constant token value ([current-page:title]) and manage it in display
    – Nicoschi
    Jan 10, 2020 at 15:27
0

For anyone looking for an updated answer, there's a 'Manage Display' contrib module which could soon be added into Drupal 10's core. You can find it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.