View source
<?php
namespace Drupal\search_autocomplete\Plugin\views\style;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CallbackSerializer extends StylePluginBase {
protected $usesRowPlugin = TRUE;
protected $usesGrouping = TRUE;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->definition = $plugin_definition + $configuration;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
unset($form['grouping']['0']['rendered']);
unset($form['grouping']['0']['rendered_strip']);
unset($form['grouping']['0']['rendered_strip']);
unset($form['grouping']['1']);
$field_labels = $this->displayHandler
->getFieldLabels(TRUE);
$input_label_descr = (empty($field_labels) ? '<b>' . $this
->t('Warning') . ': </b> ' . $this
->t('Requires at least one field in the view.') . '<br/>' : '') . $this
->t('Select the autocompletion input value. If the autocompletion settings are set to auto-submit, this value will be submitted as the suggestion is selected.');
$form['input_label'] = [
'#title' => $this
->t('Input Label'),
'#type' => 'select',
'#description' => new HtmlEscapedText($input_label_descr),
'#default_value' => $this->options['input_label'],
'#disabled' => empty($field_labels),
'#required' => TRUE,
'#options' => $field_labels,
];
$input_link_descr = (empty($field_labels) ? '<b>' . $this
->t('Warning') . ': </b> ' . $this
->t('Requires at least one field in the view.') . '<br/>' : '') . $this
->t('Select the autocompletion input link. If the autocompletion settings are set to auto-redirect, this link is where the user will be redirected as the suggestion is selected.');
$form['input_link'] = [
'#title' => $this
->t('Input Link'),
'#type' => 'select',
'#description' => new HtmlEscapedText($input_link_descr),
'#default_value' => $this->options['input_link'],
'#disabled' => empty($field_labels),
'#required' => TRUE,
'#options' => $field_labels,
];
}
public function render() {
$groups = $this
->renderGrouping($this->view->result, $this->options['grouping'], TRUE);
$response = new AjaxResponse($groups);
return $response
->getContent();
}
public function renderGrouping($records, $groupings = [], $group_rendered = NULL) {
$rows = [];
$groups = [];
foreach ($records as $index => $row) {
$this->view->rowPlugin
->setRowOptions($this->options);
if (!isset($row->row_index) || $row->row_index == NULL) {
$row->row_index = $index;
}
$rendered_row = $this->view->rowPlugin
->render($row);
if ($groupings) {
foreach ($groupings as $info) {
$group_field_name = $info['field'];
$group_id = '';
$group_name = '';
if (isset($this->view->field[$group_field_name])) {
$group_id = strtolower(Html::cleanCssIdentifier($this
->getField($index, $group_field_name)));
$group_name = $this
->getField($index, $group_field_name);
}
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'] = [];
}
$rows =& $groups[$group_id]['rows'];
}
}
else {
if (empty($groups[''])) {
$groups['']['group'] = '';
$groups['']['rows'] = [];
}
$rows =& $groups['']['rows'];
}
$rows[] = $rendered_row;
}
$return = [];
foreach ($groups as $group_id => $group) {
if (isset($group['rows']) && isset($group['rows'][0])) {
$group['rows'][0]['group'] = $group['group'];
}
$return = array_merge($return, $group['rows']);
}
return $return;
}
}