From 71d51bb901eb136463b12a3d0785bfc44464a528 Mon Sep 17 00:00:00 2001
From: James Silver <james.silver@computerminds.co.uk>
Date: Fri, 16 Sep 2011 09:40:17 +0100
Subject: [PATCH] Issue #1281272: Make inline admin links into contextual links

---
 field_collection.module |   71 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/field_collection.module b/field_collection.module
index b7dc4f6..783c7f2 100644
--- a/field_collection.module
+++ b/field_collection.module
@@ -746,25 +746,28 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i
       $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css';
       foreach ($items as $delta => $item) {
         if ($field_collection = field_collection_field_get_entity($item)) {
+          
           $element[$delta]['entity'] = $field_collection->view();
           $element[$delta]['#theme_wrappers'] = array('field_collection_view');
           $element[$delta]['#attributes']['class'][] = 'field-collection-view';
           $element[$delta]['#attributes']['class'][] = 'clearfix';
-
-          $links = array(
-            '#theme' => 'links__field_collection_view',
+          
+          // Useful data for those interested in altering
+          $element[$delta]['#entity_type'] = $entity_type;
+          $element[$delta]['#field'] = &$field;
+          $element[$delta]['#instance'] = &$instance;
+          $element[$delta]['#display'] = &$display;
+          $element[$delta]['#field_collection_item'] = $field_collection;
+          
+          // Add admin links as contextual links
+          $element[$delta]['contextual_links'] = array(
+            '#type' => 'contextual_links',
+            '#contextual_links' => array(
+              'field_collection' => array(field_collection_field_get_path($field_collection->fieldInfo()), array($field_collection->item_id)),
+            ),
+            '#element' => $element[$delta],
           );
-          $links['#attributes']['class'][] = 'field-collection-view-links';
-          foreach (array('edit', 'delete') as $op) {
-            if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) {
-              $links['#links'][$op] = array(
-                'title' => $settings[$op],
-                'href' => $field_collection->path() . '/' . $op,
-                'query' => drupal_get_destination(),
-              );
-            }
-          }
-          $element[$delta]['links'] = $links;
+          $element[$delta]['#attributes']['class'][] = 'contextual-links-region';
         }
       }
       break;
@@ -776,6 +779,46 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i
 }
 
 /**
+ * Implements hook_contextual_links_view_alter().
+ * Customize our contextual links as per our display settings.
+ * 
+ * (contextual.module should probably provide a better way of adding/customizing
+ * these links - currently it's very tied to the menu system).
+ */
+function field_collection_contextual_links_view_alter(&$element, &$items) {
+  // We only want our own contextual links
+  if (isset($element['#element']['#field']['type']) && $element['#element']['#field']['type'] === 'field_collection') {
+    
+    if (!empty($element['#element']['#display']['settings']) && !empty($element['#links'])) {
+      
+      $display_settings = $element['#element']['#display']['settings'];
+      
+      foreach ($element['#links'] as $key => $link) {
+        
+        // The keys of the links have the form: MODULENAME-LASTPATHSEGMENT
+        // @see menu_contextual_links() in menu.inc and
+        // @see contextual_pre_render_links() in contextual.module.
+        if (strpos($key, 'field-collection-') === 0) {
+          $op = substr($key, strlen('field-collection-'));
+          if (isset($display_settings[$op])) {
+            
+            // Empty string indicates user does not want the link at all
+            if ($display_settings[$op] === '') {
+              unset($element['#links'][$key]);
+            }
+            // Otherwise just override the title with the setting
+            else {
+              $element['#links'][$key]['title'] = $display_settings[$op];
+            }
+          }
+        }
+      }
+    }
+    
+  }
+}
+
+/**
  * Helper function to add links to a field collection field.
  */
 function field_collection_field_formatter_links(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) {
-- 
1.7.1

