Skip to content

Instantly share code, notes, and snippets.

@aastrong
Created April 16, 2015 17:15
Show Gist options
  • Save aastrong/9dda2b4a8a53de3a8eb2 to your computer and use it in GitHub Desktop.
Save aastrong/9dda2b4a8a53de3a8eb2 to your computer and use it in GitHub Desktop.
Patch to views limit grouping to make a secondary group
diff --git a/views-limit-grouping.tpl.php b/views-limit-grouping.tpl.php
index 82064b3..f93dcbd 100755
--- a/views-limit-grouping.tpl.php
+++ b/views-limit-grouping.tpl.php
@@ -5,13 +5,13 @@
* Basically, just a copy of views-view-unformatted.tpl.php.
*/
?>
-<div class="views-limit-grouping-group">
+<details class="views-limit-grouping-group">
<?php if (!empty($title)): ?>
- <h3><?php print $title; ?></h3>
+ <summary><span class="title"><?php print $title; ?></span></summary>
<?php endif; ?>
+ <div>
<?php foreach ($rows as $id => $row): ?>
- <div class="views-row views-row-<?php print $zebra; ?> <?php print $classes[$id]; ?>">
<?php print $row; ?>
- </div>
<?php endforeach; ?>
-</div>
+ </div>
+</details>
diff --git a/views_limit_grouping.info b/views_limit_grouping.info
index 24aebde..ed53d9b 100755
--- a/views_limit_grouping.info
+++ b/views_limit_grouping.info
@@ -2,16 +2,23 @@ name = Views Grouping Row Limit
description = "Limit the number of rows under each grouping field in a view"
; Core version (required)
-core = 6.x
+core = 7.x
; Package name (see http://drupal.org/node/101009 for a list of names)
package = Views
+files[] = views_limit_grouping.module
+files[] = views-limit-grouping.tpl.php
+files[] = views_limit_grouping_style_plugin.inc
+files[] = views_limit_grouping.views.inc
+files[] = tests/views_limit_grouping.test
+
; Module dependencies
dependencies[] = views
-; Information added by drupal.org packaging script on 2010-03-17
-version = "6.x-1.x-dev"
-core = "6.x"
+
+; Information added by drupal.org packaging script on 2013-10-19
+version = "7.x-1.x-dev"
+core = "7.x"
project = "views_limit_grouping"
-datestamp = "1268785165"
+datestamp = "1382164904"
diff --git a/views_limit_grouping.module b/views_limit_grouping.module
index 7d578b4..242be68 100755
--- a/views_limit_grouping.module
+++ b/views_limit_grouping.module
@@ -10,6 +10,6 @@
*/
function views_limit_grouping_views_api() {
return array(
- 'api' => 2,
+ 'api' => 3,
);
-}
\ No newline at end of file
+}
diff --git a/views_limit_grouping_style_plugin.inc b/views_limit_grouping_style_plugin.inc
index 63c67c6..3d5bbeb 100755
--- a/views_limit_grouping_style_plugin.inc
+++ b/views_limit_grouping_style_plugin.inc
@@ -7,70 +7,130 @@
class views_limit_grouping_style_plugin extends views_plugin_style {
- /**
- * Add our option definitions.
- */
- function option_definition() {
- $options = parent::option_definition();
- $options['grouping-limit'] = array('default' => 0);
- $options['grouping-offset'] = array('default' => 0);
- return $options;
- }
-
/**
+ * Overrides parent::options_form().
+ *
* Add our options to the form.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
- $form['grouping-limit'] = array(
- '#type' => 'textfield',
- '#title' => t('Items to display'),
- '#default_value' => $this->options['grouping-limit'],
- '#size' => 6,
- '#element_validate' => array('grouping_validate'),
- '#description' => t('The number of rows to show under each grouping header (only works when "Items to display" in the main view is set to unlimited).'),
- );
- $form['grouping-offset'] = array(
- '#type' => 'textfield',
- '#title' => t('Offset'),
- '#default_value' => $this->options['grouping-offset'],
- '#size' => 6,
- '#element_validate' => array('grouping_validate'),
- '#description' => t('The row to start on (<em>0</em> means it will start with first row, <em>1</em> means an offset of 1, and so on).'),
- );
+ foreach ($form['grouping'] as $index => $info) {
+ $defaults = $this->grouping_limit_settings($index);
+
+ $form['grouping'][$index]['grouping-limit'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Limit for grouping field Nr.!num', array('!num' => $index + 1)),
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#states' => array('invisible' => array(
+ "select[name=\"style_options[grouping][{$index}][field]\"]" => array('value' => ''),
+ )),
+ 'grouping-limit' => array(
+ '#type' => 'textfield',
+ '#title' => t('Items to display:'),
+ '#default_value' => isset($defaults['grouping-limit']) ? $defaults['grouping-limit'] : 0,
+ '#size' => 6,
+ '#element_validate' => array('grouping_validate'),
+ '#description' => t('The number of rows to show under each grouping header (only works when "Items to display" in the main view is set to unlimited).'),
+ ),
+ 'grouping-offset' => array(
+ '#type' => 'textfield',
+ '#title' => t('Offset:'),
+ '#default_value' => isset($defaults['grouping-offset']) ? $defaults['grouping-offset'] : 0,
+ '#size' => 6,
+ '#element_validate' => array('grouping_validate'),
+ '#description' => t('The row to start on (<em>0</em> means it will start with first row, <em>1</em> means an offset of 1, and so on).'),
+ ),
+ );
+ $form['group-limit'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Groups to display'),
+ '#default_value' => isset($this->options['group-limit']) ? $this->options['group-limit'] : 0,
+ '#size' => 6,
+ '#element_validate' => array('grouping_validate'),
+ '#description' => t('The number of groups to show.'),
+ );
+ $form['group-offset'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Groups offset'),
+ '#default_value' => isset($this->options['group-offset']) ? $this->options['group-offset'] : 0,
+ '#size' => 6,
+ '#element_validate' => array('grouping_validate'),
+ '#description' => t('The group to start on (<em>0</em> means it will start with first group, <em>1</em> means an offset of 1, and so on).'),
+ );
+ }
}
/**
- * Overwriting parent's render_grouping method.
+ * Overrides parent::render_grouping().
*/
- function render_grouping($records, $grouping_field = '') {
- $this->render_fields($this->view->result);
- $sets = array();
- if ($grouping_field) {
- foreach ($records as $index => $row) {
- $grouping = '';
- if (isset($this->view->field[$grouping_field])) {
- $grouping = $this->get_field($index, $grouping_field);
- if ($this->view->field[$grouping_field]->options['label']) {
- $grouping = $this->view->field[$grouping_field]->options['label'] . ': ' . $grouping;
- }
- }
- $sets[$grouping][$index] = $row;
- }
- }
- else {
- $sets[''] = $records;
- }
+ function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
+ $sets = parent::render_grouping($records, $groupings, $group_rendered);
// Apply the offset and limit.
- foreach ($sets as $group => $rows) {
- $output[$group] = array_slice($rows, $this->options['grouping-offset'], $this->options['grouping-limit'], TRUE);
+ $group_limit_count = count($sets);
+ $group_offset_start = 0;
+ if(isset($this->options['group-limit'])) {
+ $group_limit_count = ($this->options['group-limit'] == 0) ? count($sets) : $this->options['group-limit'];
+ }
+ if(isset($this->options['group-offset'])) {
+ $group_offset_start = $this->options['group-offset'];
}
+ $sets = array_slice($sets, $group_offset_start, $group_limit_count, TRUE);
+ array_walk($sets, array($this, 'group_limit_recursive'));
+ return $sets;
+ }
- return $output;
+ /**
+ * Recursively limits the number of rows in nested groups.
+ *
+ * @param array $group_data
+ * A single level of grouped records.
+ *
+ * @param mixed $key
+ * The key of the array being passed in. Used for when this function is
+ * called from array_walk() and the like. Do not set directly.
+ *
+ * @params int $level
+ * The current level we are gathering results for. Used for recursive
+ * operations; do not set directly.
+ *
+ * @return array
+ * An array with a "rows" property that is recursively grouped by the
+ * grouping fields.
+ */
+ function group_limit_recursive(&$group_data, $key = NULL, $level = 0) {
+ $settings = $this->grouping_limit_settings($level);
+ if($settings == null)return;
+
+ // Slice up the rows according to the offset and limit.
+ $subgroup_limit_count = ($settings['grouping-limit'] == 0) ? count($group_data['rows']) : $settings['grouping-limit'];
+ $group_data['rows'] = array_slice($group_data['rows'], $settings['grouping-offset'], $subgroup_limit_count, TRUE);
+
+ // For each row, if it appears to be another grouping, recurse again.
+ foreach ($group_data['rows'] as &$data) {
+ if (is_array($data) && isset($data['group']) && isset($data['rows'])) {
+ $this->group_limit_recursive($data, NULL, $level + 1);
+ }
+ }
}
-} // end of class definition
+ /**
+ * Helper function to retrieve settings for grouping limit.
+ *
+ * @param int $index
+ * The grouping level to fetch settings for.
+ *
+ * @return array
+ * Settings for this grouping level.
+ */
+ function grouping_limit_settings($index) {
+ $result = null;
+ if(isset($this->options['grouping'][$index]['grouping-limit']))
+ $result = $this->options['grouping'][$index]['grouping-limit'];
+ return $result;
+ }
+}
/**
* Validate the added form elements.
@@ -85,4 +145,4 @@ function grouping_validate($element, &$form_state) {
if ($element['#value'] < 0) {
form_error($element, t('%element cannot be negative.', array('%element' => $element['#title'])));
}
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment