View source
<?php
namespace Drupal\vefl\Plugin\views\exposed_form;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\vefl\Vefl;
trait VeflTrait {
protected $vefl;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Vefl $vefl) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vefl = $vefl;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('vefl.layout'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['layout'] = [
'contains' => [
'layout_id' => [
'default' => 'vefl_onecol',
],
'regions' => [
'default' => [],
],
'widget_region' => [
'default' => [],
],
],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$layout_id = $this->options['layout']['layout_id'];
$layouts = $this->vefl
->getLayouts();
$form['layout'] = [
'#type' => 'details',
'#title' => $this
->t('Layout settings'),
];
$form['layout']['layout_id'] = [
'#prefix' => '<div class="container-inline">',
'#type' => 'select',
'#options' => $this->vefl
->getLayoutOptions($layouts),
'#title' => $this
->t('Layout'),
'#default_value' => $layout_id,
];
$form['layout']['change'] = [
'#type' => 'submit',
'#value' => $this
->t('Change'),
'#submit' => [
[
$this,
'updateRegions',
],
],
'#suffix' => '</div>',
];
$form['layout']['widget_region'] = $this
->getRegionElements($layout_id, $layouts);
}
private function getRegionElements($layout_id, array $layouts = []) {
$element = [
'#prefix' => '<div id="edit-block-region-wrapper">',
'#suffix' => '</div>',
];
$types = [
'filters' => $this->view->display_handler
->getHandlers('filter'),
'actions' => $this->vefl
->getFormActions(),
];
$regions = [];
foreach ($layouts[$layout_id]
->getRegions() as $region_id => $region) {
$regions[$region_id] = $region['label'];
}
foreach ($types as $type => $fields) {
foreach ($fields as $id => $filter) {
if ($type == 'filters') {
if (!$filter->options['exposed']) {
continue;
}
elseif ($filter->options['is_grouped']) {
$id = $filter->options['group_info']['identifier'];
$label = $filter->options['group_info']['label'];
}
else {
$id = $filter->options['expose']['identifier'];
$label = $filter->options['expose']['label'];
}
}
else {
$label = $filter;
}
if (isset($filter->options['expose']['use_operator']) && $filter->options['expose']['use_operator']) {
$operator_id = $filter->options['expose']['operator_id'];
$element[$operator_id] = $this
->createSelectElementForVeflForm($operator_id, $this
->t('Expose operator') . ' - ' . $label, $regions);
}
$element[$id] = $this
->createSelectElementForVeflForm($id, $label, $regions);
}
}
return $element;
}
public function updateRegions($form, FormStateInterface $form_state) {
$view = $form_state
->get('view');
$display_id = $form_state
->get('display_id');
$display =& $view
->getExecutable()->displayHandlers
->get($display_id);
$display
->optionsOverride($form, $form_state);
$display
->submitOptionsForm($form, $form_state);
$view
->cacheSet();
$form_state
->set('rerender', TRUE);
$form_state
->setRebuild();
}
public function exposedFormAlter(&$form, FormStateInterface $form_state) {
parent::exposedFormAlter($form, $form_state);
$view = $form_state
->get('view');
$layout_id = $this->options['layout']['layout_id'];
$widget_region = $this->options['layout']['widget_region'];
$form['#vefl_configuration'] = [
'layout' => [
'id' => $layout_id,
'settings' => [],
],
'regions' => [],
];
foreach ($widget_region as $field_name => $region) {
$form['#vefl_configuration']['regions'][$region][] = $field_name;
if (substr($layout_id, 0, 3) == 'ds_') {
$form['#vefl_configuration']['layout']['settings']['wrappers'][$region] = 'div';
}
}
$form['#theme'] = $view
->buildThemeFunctions('vefl_views_exposed_form');
}
private function createSelectElementForVeflForm($element_id, $label, array $regions) {
$element = [
'#type' => 'select',
'#title' => $label,
'#options' => $regions,
];
if (!empty($this->options['layout']['widget_region'][$element_id]) && !empty($regions[$this->options['layout']['widget_region'][$element_id]])) {
$element['#default_value'] = $this->options['layout']['widget_region'][$element_id];
}
return $element;
}
}