You are here

public function EntityViewController::buildTitle in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::buildTitle()

Pre-render callback to build the page title.

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 64

Class

EntityViewController
Defines a generic controller to render a single entity.

Namespace

Drupal\Core\Entity\Controller

Code

public function buildTitle(array $page) {
  $entity_type = $page['#entity_type'];
  $entity = $page['#' . $entity_type];

  // If the entity's label is rendered using a field formatter, set the
  // rendered title field formatter as the page title instead of the default
  // plain text title. This allows attributes set on the field to propagate
  // correctly (e.g. in-place editing).
  if ($entity instanceof FieldableEntityInterface) {
    $label_field = $entity
      ->getEntityType()
      ->getKey('label');
    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;
}