You are here

protected function EntityViewBuilder::addContextualLinks in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::addContextualLinks()
  2. 10 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::addContextualLinks()

Add contextual links.

Parameters

array $build: The render array that is being created.

\Drupal\Core\Entity\EntityInterface $entity: The entity to be prepared.

1 call to EntityViewBuilder::addContextualLinks()
EntityViewBuilder::buildMultiple in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds multiple entities' views; augments entity defaults.

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 366

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

protected function addContextualLinks(array &$build, EntityInterface $entity) {
  if ($entity
    ->isNew()) {
    return;
  }
  $key = $entity
    ->getEntityTypeId();
  $rel = 'canonical';
  if ($entity instanceof ContentEntityInterface && !$entity
    ->isDefaultRevision()) {
    $rel = 'revision';
    $key .= '_revision';
  }
  if ($entity
    ->hasLinkTemplate($rel)) {
    $build['#contextual_links'][$key] = [
      'route_parameters' => $entity
        ->toUrl($rel)
        ->getRouteParameters(),
    ];
    if ($entity instanceof EntityChangedInterface) {
      $build['#contextual_links'][$key]['metadata'] = [
        'changed' => $entity
          ->getChangedTime(),
      ];
    }
  }
}