From 8187125918344bf909bf25c13f9298612e770296 Mon Sep 17 00:00:00 2001
From: Axel Rutz <axel.rutz@clever-systems.net>
Date: Thu, 22 May 2014 02:04:50 +0200
Subject: [PATCH] Issue #1608920 by dawehner, helior, axel.rutz | Crell: Add
 drop-button field display.

---
 handlers/views_handler_field_contextual_links.inc |   65 +++--------------
 handlers/views_handler_field_ctools_dropdown.inc  |   55 +++++++++++++++
 handlers/views_handler_field_links.inc            |   77 +++++++++++++++++++++
 modules/views.views.inc                           |    8 +++
 views.info                                        |    2 +
 5 files changed, 151 insertions(+), 56 deletions(-)
 create mode 100644 handlers/views_handler_field_ctools_dropdown.inc
 create mode 100644 handlers/views_handler_field_links.inc

diff --git a/handlers/views_handler_field_contextual_links.inc b/handlers/views_handler_field_contextual_links.inc
index faeeedc..4386e05 100644
--- a/handlers/views_handler_field_contextual_links.inc
+++ b/handlers/views_handler_field_contextual_links.inc
@@ -10,35 +10,7 @@
  *
  * @ingroup views_field_handlers
  */
-class views_handler_field_contextual_links extends views_handler_field {
-  function option_definition() {
-    $options = parent::option_definition();
-
-    $options['fields'] = array('default' => array());
-    $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
-
-    return $options;
-  }
-
-  function options_form(&$form, &$form_state) {
-    $all_fields = $this->view->display_handler->get_field_labels();
-    // Offer to include only those fields that follow this one.
-    $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
-    $form['fields'] = array(
-      '#type' => 'checkboxes',
-      '#title' => t('Fields'),
-      '#description' => t('Fields to be included as contextual links.'),
-      '#options' => $field_options,
-      '#default_value' => $this->options['fields'],
-    );
-    $form['destination'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Include destination'),
-      '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
-      '#default_value' => $this->options['destination'],
-    );
-  }
-
+class views_handler_field_contextual_links extends views_handler_field_links {
   function pre_render(&$values) {
     // Add a row plugin css class for the contextual link.
     $class = 'contextual-links-region';
@@ -50,35 +22,18 @@ class views_handler_field_contextual_links extends views_handler_field {
     }
   }
 
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+
+    $form['fields']['#description'] = t('Fields to be included as contextual links.');
+    $form['destination']['#description'] = t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.');
+  }
+
   /**
    * Render the contextual fields.
    */
   function render($values) {
-    $links = array();
-    foreach ($this->options['fields'] as $field) {
-      if (empty($this->view->style_plugin->rendered_fields[$this->view->row_index][$field])) {
-        continue;
-      }
-      $title = $this->view->field[$field]->last_render_text;
-      $path = '';
-      if (!empty($this->view->field[$field]->options['alter']['path'])) {
-        $path = $this->view->field[$field]->options['alter']['path'];
-      }
-      if (!empty($title) && !empty($path)) {
-        // Make sure that tokens are replaced for this paths as well.
-        $tokens = $this->get_render_tokens(array());
-        $path = strip_tags(decode_entities(strtr($path, $tokens)));
-
-        $links[$field] = array(
-          'href' => $path,
-          'title' => $title,
-        );
-        if (!empty($this->options['destination'])) {
-          $links[$field]['query'] = drupal_get_destination();
-        }
-      }
-    }
-
+    $links = $this->get_links();
     if (!empty($links)) {
       $build = array(
         '#prefix' => '<div class="contextual-links-wrapper">',
@@ -97,6 +52,4 @@ class views_handler_field_contextual_links extends views_handler_field {
       return '';
     }
   }
-
-  function query() { }
 }
diff --git a/handlers/views_handler_field_ctools_dropdown.inc b/handlers/views_handler_field_ctools_dropdown.inc
new file mode 100644
index 0000000..8964f88
--- /dev/null
+++ b/handlers/views_handler_field_ctools_dropdown.inc
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @file
+ * Definition of views_handler_field_ctools_dropdown.
+ */
+
+/**
+ * Field handler which displays some amount of links as ctools dropdown button.
+ *
+ * @ingroup views_field_handlers
+ */
+class views_handler_field_ctools_dropdown extends views_handler_field_links {
+  function option_definition() {
+    $options = parent::option_definition();
+
+    $options['views_admin_css'] = array('default' => TRUE, 'bool' => TRUE);
+
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    $form['fields']['#description'] = t('Fields to be included as ctools dropdown button.');
+    $form['destination']['#description'] = t('Include a "destination" parameter in the link to return the user to the original view upon completing a link action.');
+
+    $form['views_admin_css'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Include Views admin CSS'),
+      '#description' => t("Add additional css to match the style of the Views admin screen."),
+      '#default_value' => $this->options['views_admin_css'],
+    );
+  }
+
+  /**
+   * Render the dropdown button.
+   */
+  function render($values) {
+    static $added_admin_css;
+    $links = $this->get_links();
+
+    if (!empty($links)) {
+      if (!empty($this->options['views_admin_css']) && !$added_admin_css) {
+        views_include('admin');
+        views_ui_add_admin_css();
+        $added_admin_css = TRUE;
+      }
+
+      return theme('links__ctools_dropbutton', array('links' => $links, 'attributes' => array('class' => array('links', 'inline'))));
+    }
+    else {
+      return '';
+    }
+  }
+}
diff --git a/handlers/views_handler_field_links.inc b/handlers/views_handler_field_links.inc
new file mode 100644
index 0000000..bd85178
--- /dev/null
+++ b/handlers/views_handler_field_links.inc
@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * @file
+ * Definition of views_handler_field_links.
+ */
+
+/**
+ * A abstract handler which provides a collection of links.
+ *
+ * @ingroup views_field_handlers
+ */
+class views_handler_field_links extends views_handler_field {
+  function option_definition() {
+    $options = parent::option_definition();
+
+    $options['fields'] = array('default' => array());
+    $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
+
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    $all_fields = $this->view->display_handler->get_field_labels();
+    // Offer to include only those fields that follow this one.
+    $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
+    $form['fields'] = array(
+      '#type' => 'checkboxes',
+      '#title' => t('Fields'),
+      '#description' => t('Fields to be included as links.'),
+      '#options' => $field_options,
+      '#default_value' => $this->options['fields'],
+    );
+    $form['destination'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Include destination'),
+      '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
+      '#default_value' => $this->options['destination'],
+    );
+  }
+
+  /**
+   * Return the list of links of this field.
+   *
+   * @return array
+   *   The links which are used by the render function.
+   */
+  function get_links() {
+    $links = array();
+    foreach ($this->options['fields'] as $field) {
+      if (empty($this->view->field[$field]->last_render_text)) {
+        continue;
+      }
+      $title = $this->view->field[$field]->last_render_text;
+      $path = NULL;
+      if (!empty($this->view->field[$field]->options['alter']['path'])) {
+        $path = $this->view->field[$field]->options['alter']['path'];
+        // Make sure that tokens are replaced for this paths as well.
+        $tokens = $this->get_render_tokens(array());
+        $path = strip_tags(decode_entities(strtr($path, $tokens)));
+      }
+
+      $links[$field] = array(
+        'href' => $path,
+        'title' => $title,
+      );
+      if (!empty($this->options['destination']) && empty($this->view->field[$field]->options['alter']['external'])) {
+        $links[$field]['query'] = drupal_get_destination();
+      }
+    }
+
+    return $links;
+  }
+
+  function query() { }
+}
diff --git a/modules/views.views.inc b/modules/views.views.inc
index 7c3f4db..ce3aad3 100644
--- a/modules/views.views.inc
+++ b/modules/views.views.inc
@@ -99,6 +99,14 @@ function views_views_data() {
     );
   }
 
+  $data['views']['ctools_dropdown'] = array(
+    'title' => t('Dropdown links'),
+    'help' => t('Displays fields in a dropdown list, like on the views listing page.'),
+    'field' => array(
+      'handler' => 'views_handler_field_ctools_dropdown',
+    ),
+  );
+
   $data['views']['combine'] = array(
    'title' => t('Combine fields filter'),
     'help' => t('Combine two fields together and search by them.'),
diff --git a/views.info b/views.info
index abfe5df..279adeb 100644
--- a/views.info
+++ b/views.info
@@ -27,9 +27,11 @@ files[] = handlers/views_handler_field.inc
 files[] = handlers/views_handler_field_counter.inc
 files[] = handlers/views_handler_field_boolean.inc
 files[] = handlers/views_handler_field_contextual_links.inc
+files[] = handlers/views_handler_field_ctools_dropdown.inc
 files[] = handlers/views_handler_field_custom.inc
 files[] = handlers/views_handler_field_date.inc
 files[] = handlers/views_handler_field_entity.inc
+files[] = handlers/views_handler_field_links.inc
 files[] = handlers/views_handler_field_markup.inc
 files[] = handlers/views_handler_field_math.inc
 files[] = handlers/views_handler_field_numeric.inc
-- 
1.7.9.5

