From 9b5da5c9a3d5f9ab5461b234966802eea8669f12 Mon Sep 17 00:00:00 2001 From: Daniel Wehner <daniel@tag1consulting.com> Date: Mon, 11 Jan 2016 17:19:46 +0100 Subject: [PATCH] Provide contextual links for all entity types --- src/EntityViewBuilder.php | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/EntityViewBuilder.php diff --git a/src/EntityViewBuilder.php b/src/EntityViewBuilder.php new file mode 100644 index 0000000..9467c8c --- /dev/null +++ b/src/EntityViewBuilder.php @@ -0,0 +1,42 @@ +<?php + +/** + * @file + * Contains \Drupal\entity\EntityViewBuilder. + */ + +namespace Drupal\entity; + +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityViewBuilder as CoreEntityViewBuilder; + +/** + * Provides a entity view builder with contextual links support + */ +class EntityViewBuilder extends CoreEntityViewBuilder { + + /** + * {@inheritdoc} + */ + protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { + $entity_type_id = $entity->getEntityTypeId(); + if (($entity instanceof ContentEntityInterface && $entity->isDefaultRevision()) || !$entity->getEntityType()->isRevisionable()) { + $build['#contextual_links'][$entity_type_id] = [ + 'route_parameters' => [ + $entity_type_id => $entity->id() + ], + ]; + } + else { + $build['#contextual_links'][$entity_type_id . '_revision'] = [ + 'route_parameters' => [ + $entity_type_id => $entity->id(), + $entity_type_id . '_revision' => $entity->getRevisionId(), + ], + ]; + } + } + +} -- GitLab