function EntityViewController::buildTitle
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::buildTitle()
- 10 core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::buildTitle()
- 11.x core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::buildTitle()
Pre-render callback to build the page title.
There are two possibilities, depending on the value of the additional entity type property 'enable_page_title_template'.
- FALSE (default): use the output of the related field formatter if it exists. This approach only works correctly for the node entity type and with the 'string' formatter. In other cases it likely produces illegal markup and possibly incorrect display. This option has been retained for backward-compatibility to support sites that expect attributes set on the field to propagate to the page title.
- TRUE: use the output from the entity_page_title template. This approach works correctly in all cases, without relying on a particular field formatter or special templates and is the preferred option for the future.
Parameters
array $page: A page render array.
Return value
array The changed page render array.
File
-
core/
lib/ Drupal/ Core/ Entity/ Controller/ EntityViewController.php, line 77
Class
- EntityViewController
- Defines a generic controller to render a single entity.
Namespace
Drupal\Core\Entity\ControllerCode
public function buildTitle(array $page) {
$entity_type = $page['#entity_type'];
$entity = $page['#' . $entity_type];
// If the entity has a label field, build the page title based on it.
if ($entity instanceof FieldableEntityInterface) {
$label_field = $entity->getEntityType()
->getKey('label');
$template_enabled = $entity->getEntityType()
->get('enable_page_title_template');
if ($label_field && $template_enabled) {
// Set page title to the output from the entity_page_title template.
$page_title = [
'#theme' => 'entity_page_title',
'#title' => $entity->label(),
'#entity' => $entity,
'#view_mode' => $page['#view_mode'],
];
$page['#title'] = $this->renderer
->render($page_title);
// Prevent output of the label field in the main content.
$page[$label_field]['#access'] = FALSE;
return $page;
}
// Set page title to the rendered title field formatter instead of
// the default plain text title.
//
// @todo https://www.drupal.org/project/drupal/issues/3015623
// Eventually delete this code and always use the first approach.
if (isset($page[$label_field])) {
// Allow templates and theme functions to generate different markup
// for the page title, which must be inline markup as it will be placed
// inside <h1>. See field--node--title.html.twig.
$page[$label_field]['#is_page_title'] = TRUE;
$page['#title'] = $this->renderer
->render($page[$label_field]);
}
}
return $page;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.