+ /**
+ * Add contextual links.
+ *
+ * @param array $build
+ * The render array that is being created.
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The entity to be prepared.
+ */
+ 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(),
+ ];
+ }
+ }
+ }
+