From e21ac4d99e10bca02afc6c741eaadbe438dbdda4 Mon Sep 17 00:00:00 2001
From: Kristiaan Van den Eynde <magentix@gmail.com>
Date: Wed, 19 Aug 2015 15:51:12 +0200
Subject: [PATCH] Issue #1598652 by joachim, fago, DamienMcKenna,
 kristiaanvandeneynde: show contextual link to edit entities

---
 includes/entity.controller.inc | 13 +++++++++++++
 includes/entity.ui.inc         |  1 +
 theme/entity.tpl.php           |  8 ++++++++
 3 files changed, 22 insertions(+)

diff --git a/includes/entity.controller.inc b/includes/entity.controller.inc
index f675a63..9ffc8e8 100644
--- a/includes/entity.controller.inc
+++ b/includes/entity.controller.inc
@@ -672,6 +672,19 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
         '#language' => $langcode,
         '#page' => $page,
       );
+
+      // Add in contextual links, but only if we are not at the entity's page as
+      // tabs are usually available there.
+      if (!$page && isset($this->entityInfo['admin ui']['path'])) {
+        $key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
+        $build['#contextual_links'] = array(
+          $this->entityInfo['module'] => array(
+            $this->entityInfo['admin ui']['path'] . '/manage',
+            array($key),
+          ),
+        );
+      }
+
       // Allow modules to modify the structured entity.
       drupal_alter(array($this->entityType . '_view', 'entity_view'), $build, $this->entityType);
       $key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
diff --git a/includes/entity.ui.inc b/includes/entity.ui.inc
index e115ad9..db8f03a 100644
--- a/includes/entity.ui.inc
+++ b/includes/entity.ui.inc
@@ -76,6 +76,7 @@ class EntityDefaultUIController {
       'title' => 'Edit',
       'load arguments' => array($this->entityType),
       'type' => MENU_DEFAULT_LOCAL_TASK,
+      'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE
     );
 
     // Clone form, a special case for the edit form.
diff --git a/theme/entity.tpl.php b/theme/entity.tpl.php
index cfe94dd..c07265e 100644
--- a/theme/entity.tpl.php
+++ b/theme/entity.tpl.php
@@ -18,6 +18,12 @@
  *   the parts enclosed by {} are replaced by the appropriate values:
  *   - entity-{ENTITY_TYPE}
  *   - {ENTITY_TYPE}-{BUNDLE}
+ * - $title_prefix (array): An array containing additional output populated by
+ *   modules, intended to be displayed in front of the main title tag that
+ *   appears in the template.
+ * - $title_suffix (array): An array containing additional output populated by
+ *   modules, intended to be displayed after the main title tag that appears in
+ *   the template.
  *
  * Other variables:
  * - $classes_array: Array of html class attribute values. It is flattened
@@ -31,6 +37,7 @@
 <div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
 
   <?php if (!$page): ?>
+    <?php print render($title_prefix); ?>
     <h2<?php print $title_attributes; ?>>
       <?php if ($url): ?>
         <a href="<?php print $url; ?>"><?php print $title; ?></a>
@@ -38,6 +45,7 @@
         <?php print $title; ?>
       <?php endif; ?>
     </h2>
+    <?php print render($title_suffix); ?>
   <?php endif; ?>
 
   <div class="content"<?php print $content_attributes; ?>>
-- 
2.0.1

