From 03f78b0a808668f02a0e91715513d47c9b75e3b4 Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Sat, 26 May 2012 14:45:32 -0500
Subject: [PATCH] Added contextual links.

---
 viewreference.module |  169 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 110 insertions(+), 59 deletions(-)

diff --git a/viewreference.module b/viewreference.module
index f04f22e..392a52a 100755
--- a/viewreference.module
+++ b/viewreference.module
@@ -425,118 +425,169 @@ function viewreference_views_display_title($view_name, $view, $display) {
 }
 
 /**
- * Get an array of data and rendered HTML items that are useful in theming the
+ * Return an array of data and rendered HTML items that are useful in theming the
  * formatter output.
  *
  * @param $element
  *   An array of parameters used in viewing the element.
- * @param $params
- *   An array of special features needed to be built:
- *     'embed' - The $view->preview() of the view display
- *     'title' - The display title of the view display
- *     'link' - A link to the view display, or the title if link not available.
+ * @param $build
+ *   An array of strings which contains the desired elements of the view that should be built:
+ *     'link' - A link to the view display using the title for display text, or the title if link not available.
+ *     'path' - The path to the view display, or the title if path not available.
+ *     'title' - The display title of the view display.
+ *     'view' - The $view->preview() of the view display. Includes legacy support for previous 'embed' build parameter.
  * @return
  *   The array of useful data about the view.
  */
-function viewreference_get_view($element, $params = array()) {
-  if (!empty($element['item']['view_id'])) {
-    $view_id_parts = explode(':', $element['item']['view_id']);
-    $view['name'] = $view_id_parts[0];
-    $view['display'] = $view_id_parts[1];
-    $view['view'] = views_get_view($view['name']);
-    if ($view['view'] && $view['view']->access($view['display'])) {
-      // Save $_GET['q'] so it can be restored before returning from this function.
-      $q = $_GET['q'];
-      if (in_array('embed', $params)) {
-        $view['args'] = viewreference_get_element_args($element);
-        if (!empty($view['args'])) {
-          $_GET['q'] .= '/' . implode('/', $view['args']);
+function viewreference_get_view($element, $build = array()) {
+  $output = FALSE;
+  // Return FALSE immediately if there is no view id set in the element.
+  if (!isset($element['item']['view_id']) || empty($element['item']['view_id'])) {
+    return $output;
+  }
+  // Extract the name and display of the view.
+  list($name, $display) = explode(':', $element['item']['view_id']);
+  // Attempt to load the view.
+  $view = views_get_view($name);
+  // Determine if view exists and user has access to it.
+  if ($view && $view->access($display)) {
+    // Convert $build to an array if only a string was provided.
+    if (!is_array($build)) {
+      $build = array($build);
+    }
+    // Clone the value to the key in the build array.
+    $build = array_combine($build, $build);
+    // Default output array.
+    $output = array();
+    // Ensure the keys are set to return at least an empty string.
+    foreach ($build as $value) {
+      $output[$value] = '';
+    }
+    // Preserve the original $_GET['q'] variable for the remainder of this function.
+    $q = $_GET['q'];
+    // Extract the arguments provided for the view and attach them to end of $_GET['q'].
+    $output['args'] = viewreference_get_element_args($element);
+    if (!empty($output['args'])) {
+      $_GET['q'] .= '/' . implode('/', $output['args']);
+    }
+    // Generate the title of the view display for $build['title'] and $build['link'].
+    if (isset($build['title']) || isset($build['link'])) {
+      $output['title'] = theme('viewreference_display_title', array(
+        'view' => $view,
+        'view_name' => $name,
+        'display_key' => $display,
+      ));
+    }
+    // Generate the preview content of the view display for $build['view'] and legacy $build['embed'].
+    if (isset($build['embed']) || isset($build['view'])) {
+      $output[isset($build['embed']) ? 'embed' : 'view'] = $view->preview($display, $output['args']);
+    }
+    // Generate the link or path of the view display for $build['link'] and $build['path'].
+    if (isset($build['link']) || isset($build['path'])) {
+      $output['disabled'] = isset($view->disabled) ? $view->disabled : FALSE;
+      if (!$output['disabled'] && isset($view->display[$display]->display_options['path'])) {
+        $output['path'] = $view->display[$display]->display_options['path'];
+        $url_args = implode('/', $output['args']);
+        if (!empty($url_args)) {
+          $output['path'] .= '/' . $url_args;
         }
-        $view['embed'] = $view['view']->preview($view['display'], $view['args']);
-      }
-      if (in_array('title', $params) || in_array('link', $params)) {
-        $view['title'] = theme('viewreference_display_title', $view);
-        if (in_array('link', $params)) {
-          $disabled = isset($view['view']->disabled) ? $view['view']->disabled:  FALSE;
-          if (isset($view['view']->display[$view['display']]->display_options['path']) && !$disabled) {
-            $view['args'] = isset($view['args']) ? $view['args'] : viewreference_get_element_args($element);
-            $view['url_args'] = implode('/', $view['args']);
-            $view['path'] = $view['view']->display[$view['display']]->display_options['path'];
-            if ($view['url_args']) {
-              $view['path'] .= '/' . $view['url_args'];
-            }
-            $view['link'] = l($view['title'], $view['view']->get_url($view['args'], $view['path']));
-          }
-          else {
-            $view['link'] = $view['title'];
-          }
+        if (isset($build['link'])) {
+          $output['link'] = l($output['title'], $view->get_url($output['args'], $output['path']));
         }
       }
-      $_GET['q'] = $q;
-      return $view;
+      else {
+        if (isset($build['link'])) {
+          $output['link'] = $output['title'];
+        }
+        if (isset($build['path'])) {
+          $output['path'] = $output['title'];
+        }
+      }        
+    }
+    // Generate contextual links to add to the output array.
+    if (module_exists('contextual')) {
+      $output += contextual_element_info();
+      views_add_contextual_links($output['contextual_links'], 'block', $view, $display);
     }
   }
+  return $output;
+}
+
+/**
+ * Helper function that wraps contextual links around the output of a formatter.
+ */
+function viewreference_add_contextual_links($output, $view) {
+  // Only continue if there are actual contextual links to add.
+  if ($view && isset($view['contextual_links']['#contextual_links']) && !empty($view['contextual_links']['#contextual_links'])) {
+    // Convert output to a drupal render array.
+    $output = array(
+      '#prefix' => '<div class="contextual-links-region">',
+      '#suffix' => '</div>',
+      'contextual_links' => $view['contextual_links'],
+      'content' => array(
+        '#markup' => $output,
+      ),
+    );
+    return drupal_render($output);
+  }
+  return $output;
 }
 
 /**
  * Theme function for 'default' viewreference field formatter.
  */
 function theme_viewreference_formatter_default($variables) {
-  $element = &$variables['element'];
   $output = '';
-  if ($view = viewreference_get_view($element, array('embed'))) {
-    $output .= $view['embed'];
+  if ($view = viewreference_get_view($variables['element'], 'view')) {
+    $output .= $view['view'];
   }
-  return $output;
+  return viewreference_add_contextual_links($output, $view);
 }
 
 /**
  * Theme function for 'full' viewreference field formatter.
  */
 function theme_viewreference_formatter_full($variables) {
-  $element = &$variables['element'];
   $output = '';
-  if ($view = viewreference_get_view($element, array('title', 'embed'))) {
+  if ($view = viewreference_get_view($variables['element'], array('title', 'view'))) {
     $output .= '<h3 class="title viewreference-title">' . $view['title'] . '</h3>';
-    $output .= $view['embed'];
+    $output .= $view['view'];
   }
-  return $output;
+  return viewreference_add_contextual_links($output, $view);
 }
 
 /**
  * Theme function for 'plain' viewreference field formatter.
  */
 function theme_viewreference_formatter_plain($variables) {
-  $element = &$variables['element'];
   $output = '';
-  if ($view = viewreference_get_view($element, array('title'))) {
+  if ($view = viewreference_get_view($variables['element'], 'title')) {
     $output .= $view['title'];
   }
-  return $output;
+  return viewreference_add_contextual_links($output, $view);
+  
 }
 
 /**
  * Theme function for 'link' viewreference field formatter.
  */
 function theme_viewreference_formatter_link($variables) {
-  $element = &$variables['element'];
   $output = '';
-  if ($view = viewreference_get_view($element, array('link'))) {
-    $output .= $view['link'];
+  if ($view = viewreference_get_view($variables['element'], 'link')) {
+    $output .= $view['view'];
   }
-  return $output;
+  return viewreference_add_contextual_links($output, $view);
 }
 
 /**
  * Theme function for 'path' viewreference field formatter.
  */
 function theme_viewreference_formatter_path($variables) {
-  $element = &$variables['element'];
   $output = '';
-  if ($view = viewreference_get_view($element, array('link'))) {
-    $output .= isset($view['path']) ? $view['path'] : $view['link'];
+  if ($view = viewreference_get_view($variables['element'], 'path')) {
+    $output .= $view['path'];
   }
-  return $output;
+  return viewreference_add_contextual_links($output, $view);  
 }
 
 /**
@@ -914,4 +965,4 @@ function viewreference_autocomplete_process($element, $form_state, $form) {
       $data[$table_alias][$field['field_name'] . '_view_id']['filter']['handler'] = 'views_handler_filter_many_to_one_content';
       return $data;
 
-*/
\ No newline at end of file
+*/
-- 
1.7.10.1

