From d411885aebb86d8f3165b0bf1fb5daacb42afff8 Mon Sep 17 00:00:00 2001
From: James Silver <james.silver@computerminds.co.uk>
Date: Wed, 20 Jul 2011 17:07:51 +0100
Subject: [PATCH] Add sensible contextual links

---
 cck_blocks.module |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/cck_blocks.module b/cck_blocks.module
index 4047e35..afd5df8 100644
--- a/cck_blocks.module
+++ b/cck_blocks.module
@@ -142,6 +142,10 @@ function cck_blocks_block_view($delta = '') {
 
         // Set the field's data as the content of the block
         $block['content'] = $block_content;
+        
+        // Pass node & field info to anyone who wants to modify stuff.
+        $block['node'] = $node;
+        $block['field'] = $field_data;
       }
     }
   }
@@ -149,6 +153,33 @@ function cck_blocks_block_view($delta = '') {
 }
 
 /**
+ * Implements hook_preprocess_block().
+ */
+function cck_blocks_preprocess_block(&$vars) {
+  // Borrow node.modules contextual local tasks for the parent node
+  if ($vars['block']->module == 'cck_blocks' && !empty($vars['block']->node)) {
+    $vars['elements']['#contextual_links']['cck_blocks'] = array('node', array($vars['block']->node->nid));
+  }
+}
+
+
+/**
+ * Implements hook_contextual_links_view_alter().
+ */
+function cck_blocks_contextual_links_view_alter(&$element, &$items) {
+  // Modify the contextual links to make them more intuitive.
+  if (isset($element['#links']['cck-blocks-edit'])) {
+    $element['#links']['cck-blocks-edit']['title'] = t('Edit field');
+    // We need a better way of calculating the fragment to skip the
+    // user straight down to the field they want.
+    $element['#links']['cck-blocks-edit']['fragment'] = drupal_html_id('edit-' . str_replace('_', '-', $element['#element']['#block']->field['#field_name']));
+  }
+  if (isset($element['#links']['cck-blocks-delete'])) {
+    unset($element['#links']['cck-blocks-delete']);
+  }
+}
+
+/**
  * Implements hook_form_FORM_ID_alter().
  *
  * Adds options to the field configuration page for making the field available as a
-- 
1.7.1

