function EntityViewBuilder::addContextualLinks

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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 355

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) && $entity->toUrl($rel)
        ->isRouted()) {
        $build['#contextual_links'][$key] = [
            'route_parameters' => $entity->toUrl($rel)
                ->getRouteParameters(),
        ];
        if ($entity instanceof EntityChangedInterface) {
            $build['#contextual_links'][$key]['metadata'] = [
                'changed' => $entity->getChangedTime(),
            ];
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.