View source
<?php
class views_limit_grouping_style_plugin extends views_plugin_style {
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
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).'),
),
);
}
}
function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
$sets = parent::render_grouping($records, $groupings, $group_rendered);
array_walk($sets, array(
$this,
'group_limit_recursive',
));
return $sets;
}
function group_limit_recursive(&$group_data, $key = NULL, $level = 0) {
$settings = $this
->grouping_limit_settings($level);
$group_data['rows'] = array_slice($group_data['rows'], $settings['grouping-offset'], $settings['grouping-limit'], TRUE);
foreach ($group_data['rows'] as &$data) {
if (is_array($data) && isset($data['group']) && isset($data['rows'])) {
$this
->group_limit_recursive($data, NULL, $level + 1);
}
}
}
function grouping_limit_settings($index) {
return $this->options['grouping'][$index]['grouping-limit'];
}
}
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'],
)));
}
}