public function CallbackSerializer::renderGrouping in Search Autocomplete 8
Same name and namespace in other branches
- 2.x src/Plugin/views/style/CallbackSerializer.php \Drupal\search_autocomplete\Plugin\views\style\CallbackSerializer::renderGrouping()
Group records as needed for rendering.
Parameters
$records: An array of records from the view to group.
$groupings: An array of grouping instructions on which fields to group. If empty, the result set will be given a single group with an empty string as a label.
$group_rendered: Boolean value 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. After Views 7.x-3.0 this boolean is only used if $groupings is an old-style string or if the rendered option is missing for a grouping instruction.
Return value
The grouped record set. A nested set structure is generated if multiple grouping fields are used.
array(
'grouping_field_1:grouping_1' => array(
'group' => 'grouping_field_1:content_1',
'level' => 0,
'rows' => array(
'grouping_field_2:grouping_a' => array(
'group' => 'grouping_field_2:content_a',
'level' => 1,
'rows' => array(
$row_index_1 => $row_1,
$row_index_2 => $row_2,
),
),
),
),
'grouping_field_1:grouping_2' => array(),
);
Overrides StylePluginBase::renderGrouping
1 call to CallbackSerializer::renderGrouping()
- CallbackSerializer::render in src/
Plugin/ views/ style/ CallbackSerializer.php - Render the display in this style.
File
- src/
Plugin/ views/ style/ CallbackSerializer.php, line 114
Class
- CallbackSerializer
- The style plugin for serialized output formats.
Namespace
Drupal\search_autocomplete\Plugin\views\styleCode
public function renderGrouping($records, $groupings = [], $group_rendered = NULL) {
$rows = [];
$groups = [];
// Iterate through all records for transformation.
foreach ($records as $index => $row) {
$this->view->rowPlugin
->setRowOptions($this->options);
// Render the row according to our custom needs.
if (!isset($row->row_index) || $row->row_index == NULL) {
$row->row_index = $index;
}
$rendered_row = $this->view->rowPlugin
->render($row);
// Case when it takes grouping.
if ($groupings) {
// Iterate through configured grouping field. Currently only one level
// of grouping allowed.
foreach ($groupings as $info) {
$group_field_name = $info['field'];
$group_id = '';
$group_name = '';
// Extract group data if available.
if (isset($this->view->field[$group_field_name])) {
// Extract group_id and transform it to machine name.
$group_id = strtolower(Html::cleanCssIdentifier($this
->getField($index, $group_field_name)));
// Extract group displayed value.
$group_name = $this
->getField($index, $group_field_name);
}
// Create the group if it does not exist yet.
if (empty($groups[$group_id])) {
$groups[$group_id]['group']['group_id'] = $group_id;
$groups[$group_id]['group']['group_name'] = $group_name;
$groups[$group_id]['rows'] = [];
}
// Move the set reference into the row set of the group
// we just determined.
$rows =& $groups[$group_id]['rows'];
}
}
else {
// Create the group if it does not exist yet.
if (empty($groups[''])) {
$groups['']['group'] = '';
$groups['']['rows'] = [];
}
$rows =& $groups['']['rows'];
}
// Add the row to the hierarchically positioned
// row set we just determined.
$rows[] = $rendered_row;
}
/*
* Build the result from previous array.
* @todo: find a more straight forward way to make it.
*/
$return = [];
foreach ($groups as $group_id => $group) {
// Add group info on first row lign.
if (isset($group['rows']) && isset($group['rows'][0])) {
$group['rows'][0]['group'] = $group['group'];
}
// Add rows of this group to result.
$return = array_merge($return, $group['rows']);
}
return $return;
}