Change record status: 
Project: 
Introduced in branch: 
9.4.x
Introduced in version: 
9.4.0
Description: 

If a fieldable entity has a label field, then Drupal removes the label from the normal entity rendering and instead uses it to build the page title (see EntityViewController->buildTitle()). This prevents duplicate display of the label and yet preserves any attributes added by module hooks.

Unfortunately this produces illegal and incorrect markup because the output of a field formatter is not in general suitable for display in this way.

In Drupal core, the bug occurs for the Media entity type. By default, the Node entity type is correct due to a specific workaround, but the bug occurs if the site uses a hook or module to make the title field configurable via the field UI.

Before

  1. The page title can contain illegal markup such as <h2> and <div> inside <h1>.
  2. If the page title has been hidden from display then any attributes added by module hooks are missing.

After

Developers can enable correct output by means of the following hook:

function mymodule_entity_type_build(array &$entity_types) {
  $entity_types['node']->set('enable_page_title_template', TRUE);
}

With this setting in place, then Drupal uses a new template entity-page-title.html.twig to build the page title correctly - totally independent from the output from the field formatter.

Module developers that wish to add attributes related to the label field should implement this hook, for example:

/**
 * Implements hook_preprocess_entity_page_title().
 */
function quickedit_preprocess_entity_page_title(&$variables) {
  $variables['#cache']['contexts'][] = 'user.permissions';
  $entity = $variables['entity'];
  if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
    return;
  }
  if ($entity instanceof RevisionableInterface) && !$entity->isLatestRevision()) {
    return;
  }

  $label_field = $entity->getEntityType()->getKey('label');
  $variables['attributes']['data-quickedit-field-id'] = $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $label_field . '/' . $entity->get('langcode')->value . '/' . $variables['view_mode'];
}

Template changes

Themers that wish to customize the entity page title should override the template entity-page-title.html.twig.

Future plans

For back-compatibility, the new template is only used if it has been enabled by means of a hook. The long-term intention is to always use the new template.

Impacts: 
Module developers
Themers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done