View source
<?php
class views_limit_grouping_style_plugin extends views_plugin_style {
function option_definition() {
$options = parent::option_definition();
$options['grouping-limit'] = array(
'default' => 0,
);
$options['grouping-offset'] = array(
'default' => 0,
);
return $options;
}
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).'),
);
}
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;
}
foreach ($sets as $group => $rows) {
$output[$group] = array_slice($rows, $this->options['grouping-offset'], $this->options['grouping-limit'], TRUE);
}
return $output;
}
}
function grouping_validate($element, &$form_state) {
if (!is_numeric($element['#value'])) {
form_error($element, t('%element must be numeric.', array(
'%element' => $element['#title'],
)));
}
if ($element['#value'] < 0) {
form_error($element, t('%element cannot be negative.', array(
'%element' => $element['#title'],
)));
}
}