summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--help/api-upgrading.html30
-rw-r--r--plugins/views_plugin_style.inc76
-rw-r--r--tests/styles/views_plugin_style.test35
3 files changed, 130 insertions, 11 deletions
diff --git a/help/api-upgrading.html b/help/api-upgrading.html
index 8d9a9df..158f809 100644
--- a/help/api-upgrading.html
+++ b/help/api-upgrading.html
@@ -192,3 +192,33 @@ Due to changes in comment.module there is no extra views-view-row-node template
<h3>Entity type Key on Base tables</h3>
During the development of the drupal7 version of views the entity type associated with a table got added to $data['name']['table']['base']['entity type']. It should be moved to $data['name']['table']['entity type'].
+
+<h3>Changed views_plugin_style::render_grouping()</h3>
+The parameters as well as the structure of the methods return have changed.
+The method now accepts a third optional parameter called "$group_rendered".
+This parameter defines whether to use the rendered or the raw field value for grouping.
+Intention for adding the parameter was that the grouping could have been acted
+unexpected if the rendered field contained unique values e.g. by using drupal_html_id().
+<dl>
+<dt>New return structure</dt>
+<dd>
+{grouping value} is the value affected by the new parameter.
+<pre>
+ array (
+ {grouping value} => array(
+ 'group' => {rendered_value of the grouping field},
+ 'rows' => array({group rows}),
+ ),
+ );
+</pre>
+</dd>
+<dt>Old return structure</dt>
+<dd>
+<strong>If the new parameter isn't explicitly set or its value is NULL the structure of the return will be the same as in D6!</strong>
+<pre>
+ array (
+ {rendered_value of the grouping field} => array({group rows}),
+ );
+</pre>
+</dd>
+</dl>
diff --git a/plugins/views_plugin_style.inc b/plugins/views_plugin_style.inc
index 084e65d..d347ce8 100644
--- a/plugins/views_plugin_style.inc
+++ b/plugins/views_plugin_style.inc
@@ -44,6 +44,7 @@ class views_plugin_style extends views_plugin {
$this->options += array(
'grouping' => '',
+ 'group_rendered' => TRUE,
);
$this->definition += array(
@@ -152,6 +153,7 @@ class views_plugin_style extends views_plugin {
function option_definition() {
$options = parent::option_definition();
$options['grouping'] = array('default' => '');
+ $options['group_rendered'] = array('default' => TRUE);
if ($this->uses_row_class()) {
$options['row_class'] = array('default' => '');
}
@@ -177,6 +179,12 @@ class views_plugin_style extends views_plugin {
'#default_value' => $this->options['grouping'],
'#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
);
+ $form['group_rendered'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Use rendered output to group rows'),
+ '#default_value' => $this->options['group_rendered'],
+ '#description' => t('If enabled the rendered output of the grouping field is used to group the rows.'),
+ );
}
}
@@ -229,21 +237,26 @@ class views_plugin_style extends views_plugin {
}
// Group the rows according to the grouping field, if specified.
- $sets = $this->render_grouping($this->view->result, $this->options['grouping']);
+ $sets = $this->render_grouping(
+ $this->view->result,
+ $this->options['grouping'],
+ (bool) $this->options['group_rendered']
+ );
// Render each group separately and concatenate. Plugins may override this
// method if they wish some other way of handling grouping.
$output = '';
- foreach ($sets as $title => $records) {
+ foreach ($sets as $group) {
+ $title = $group['group'];
if ($this->uses_row_plugin()) {
$rows = array();
- foreach ($records as $row_index => $row) {
+ foreach ($group['rows'] as $row_index => $row) {
$this->view->row_index = $row_index;
$rows[$row_index] = $this->row_plugin->render($row);
}
}
else {
- $rows = $records;
+ $rows = $group['rows'];
}
$output .= theme($this->theme_functions(),
@@ -266,10 +279,14 @@ class views_plugin_style extends views_plugin {
* @param $grouping_field
* The field id on which to group. If empty, the result set will be given
* a single group with an empty string as a label.
+ * @param $group_rendered
+ * Boolean value to switch whether to use the rendered or the raw field
+ * value for grouping. If set to NULL the return is structured as before
+ * Views 7.x-3.0-rc2.
* @return
* The grouped record set.
*/
- function render_grouping($records, $grouping_field = '') {
+ function render_grouping($records, $grouping_field = '', $group_rendered = NULL) {
// Make sure fields are rendered
$this->render_fields($this->view->result);
$sets = array();
@@ -280,18 +297,42 @@ class views_plugin_style extends views_plugin {
// we can control any special formatting of the grouping field through
// the admin or theme layer or anywhere else we'd like.
if (isset($this->view->field[$grouping_field])) {
- $grouping = $this->get_field($index, $grouping_field);
+ $group_content = $this->get_field($index, $grouping_field);
if ($this->view->field[$grouping_field]->options['label']) {
- $grouping = $this->view->field[$grouping_field]->options['label'] . ': ' . $grouping;
+ $group_content = $this->view->field[$grouping_field]->options['label'] . ': ' . $group_content;
+ }
+ if ($group_rendered) {
+ $grouping = $group_content;
+ }
+ else {
+ $grouping = $this->get_field_value($index, $grouping_field);
+ }
+ if (empty($sets[$grouping]['group'])) {
+ $sets[$grouping]['group'] = $group_content;
}
}
- $sets[$grouping][$index] = $row;
+ $sets[$grouping]['rows'][$index] = $row;
}
}
else {
// Create a single group with an empty grouping field.
- $sets[''] = $records;
+ $sets[''] = array(
+ 'group' => '',
+ 'rows' => $records,
+ );
+ }
+
+ // If this parameter isn't explicitely set modify the output to be fully
+ // backward compatible to code before Views 7.x-3.0-rc2.
+ // @TODO Remove this as soon as possible e.g. October 2020
+ if ($group_rendered === NULL) {
+ $old_style_sets = array();
+ foreach ($sets as $group) {
+ $old_style_sets[$group['group']] = $group['rows'];
+ }
+ $sets = $old_style_sets;
}
+
return $sets;
}
@@ -347,6 +388,21 @@ class views_plugin_style extends views_plugin {
}
}
+ /**
+ * Get the raw field value.
+ *
+ * @param $index
+ * The index count of the row.
+ * @param $field
+ * The id of the field.
+ */
+ function get_field_value($index, $field) {
+ $this->view->row_index = $index;
+ $value = $this->view->field[$field]->get_value($this->view->result[$index]);
+ unset($this->view->row_index);
+ return $value;
+ }
+
function validate() {
$errors = parent::validate();
@@ -375,4 +431,4 @@ class views_plugin_style extends views_plugin {
/**
* @}
- */
+ */ \ No newline at end of file
diff --git a/tests/styles/views_plugin_style.test b/tests/styles/views_plugin_style.test
index ac0607b..5bad590 100644
--- a/tests/styles/views_plugin_style.test
+++ b/tests/styles/views_plugin_style.test
@@ -49,6 +49,7 @@ class ViewsPluginStyleTestCase extends ViewsSqlTest {
// Now run the query and groupby the result.
$this->executeView($view);
+ // This is the old way to call it.
$sets = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping']);
$expected = array();
@@ -68,6 +69,38 @@ class ViewsPluginStyleTestCase extends ViewsSqlTest {
$expected['Job: Drummer'][2]->views_test_job = 'Drummer';
$expected['Job: Drummer'][2]->views_test_id = '3';
- $this->assertEqual($sets, $expected, t('The style plugin should prober groupby the results'));
+ $this->assertEqual($sets, $expected, t('The style plugin should proper groupby the results'));
+
+ $expected = array();
+ $expected['Job: Singer'] = array();
+ $expected['Job: Singer']['group'] = 'Job: Singer';
+ $expected['Job: Singer']['rows'][0] = new StdClass();
+ $expected['Job: Singer']['rows'][0]->views_test_name = 'John';
+ $expected['Job: Singer']['rows'][0]->views_test_job = 'Singer';
+ $expected['Job: Singer']['rows'][0]->views_test_id = '1';
+ $expected['Job: Singer']['rows'][1] = new StdClass();
+ $expected['Job: Singer']['rows'][1]->views_test_name = 'George';
+ $expected['Job: Singer']['rows'][1]->views_test_job = 'Singer';
+ $expected['Job: Singer']['rows'][1]->views_test_id = '2';
+ $expected['Job: Drummer'] = array();
+ $expected['Job: Drummer']['group'] = 'Job: Drummer';
+ $expected['Job: Drummer']['rows'][2] = new StdClass();
+ $expected['Job: Drummer']['rows'][2]->views_test_name = 'Ringo';
+ $expected['Job: Drummer']['rows'][2]->views_test_job = 'Drummer';
+ $expected['Job: Drummer']['rows'][2]->views_test_id = '3';
+
+ // The newer api passes the value of the grouping as well.
+ $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE);
+ $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE);
+
+ $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper groupby the results with grouped by the rendered output.'));
+
+ // Reorder the group structure to group by value.
+ $expected['Singer'] = $expected['Job: Singer'];
+ $expected['Drummer'] = $expected['Job: Drummer'];
+ unset($expected['Job: Singer']);
+ unset($expected['Job: Drummer']);
+
+ $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper groupby the results with grouped by the value.'));
}
}