From cebf0f07033b44b1adc1a7c8811262f6dfa2ff94 Mon Sep 17 00:00:00 2001
From: damiankloip <damiankloip@1037976.no-reply.drupal.org>
Date: Mon, 16 Jul 2012 13:39:57 +0200
Subject: [PATCH] Issue #1548948 by damiankloip: Added ability for field
 handlers to declare whether they can be 'grouped by' or not.

---
 handlers/views_handler_field.inc | 11 +++++++++++
 plugins/views_plugin_display.inc | 15 ++++++++++++++-
 plugins/views_plugin_style.inc   |  3 +--
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/handlers/views_handler_field.inc b/handlers/views_handler_field.inc
index 428e3b97bdf8..5b8cd6305495 100644
--- a/handlers/views_handler_field.inc
+++ b/handlers/views_handler_field.inc
@@ -378,6 +378,17 @@ function get_value($values, $field = NULL) {
     }
   }
 
+  /**
+   * Determines if this field will be available as an option to group the result
+   * by in the style settings.
+   *
+   * @return bool
+   *  TRUE if this field handler is groupable, otherwise FALSE.
+   */
+  function use_string_group_by() {
+    return TRUE;
+  }
+
   function option_definition() {
     $options = parent::option_definition();
 
diff --git a/plugins/views_plugin_display.inc b/plugins/views_plugin_display.inc
index 97e3660a195d..d469f5171ff4 100644
--- a/plugins/views_plugin_display.inc
+++ b/plugins/views_plugin_display.inc
@@ -975,9 +975,18 @@ function get_handlers($type) {
 
   /**
    * Retrieve a list of fields for the current display with the
-   *  relationship associated if it exists.
+   * relationship associated if it exists.
+   *
+   * @param $groupable_only
+   *  Return only an array of field labels from handler that return TRUE
+   *  from use_string_group_by method.
    */
   function get_field_labels() {
+    // Use func_get_arg so the function signature isn't amended
+    // but we can still pass TRUE into the function to filter
+    // by groupable handlers.
+    $groupable_only = func_get_arg(0);
+
     $options = array();
     foreach ($this->get_handlers('relationship') as $relationship => $handler) {
       if ($label = $handler->label()) {
@@ -989,6 +998,10 @@ function get_field_labels() {
     }
 
     foreach ($this->get_handlers('field') as $id => $handler) {
+      if ($groupable_only && !$handler->use_string_group_by()) {
+        // Continue to next handler if it's not groupable.
+        continue;
+      }
       if ($label = $handler->label()) {
         $options[$id] = $label;
       }
diff --git a/plugins/views_plugin_style.inc b/plugins/views_plugin_style.inc
index 831ba369349a..020b33f817bf 100644
--- a/plugins/views_plugin_style.inc
+++ b/plugins/views_plugin_style.inc
@@ -187,9 +187,8 @@ function options_form(&$form, &$form_state) {
     // @TODO: Document "uses grouping" in docs.php when docs.php is written.
     if ($this->uses_fields() && $this->definition['uses grouping']) {
       $options = array('' => t('- None -'));
-      $field_labels = $this->display->handler->get_field_labels();
+      $field_labels = $this->display->handler->get_field_labels(TRUE);
       $options += $field_labels;
-
       // If there are no fields, we can't group on them.
       if (count($options) > 1) {
         // This is for backward compability, when there was just a single select form.
-- 
GitLab