View source
<?php
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\user\RoleInterface;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\Component\Utility\Html;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
abstract class FilterPluginBase extends HandlerBase implements CacheableDependencyInterface {
var $value = NULL;
var $operator = '=';
var $group_info = NULL;
protected $alwaysMultiple = FALSE;
var $no_operator = FALSE;
var $always_required = FALSE;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->operator = $this->options['operator'];
$this->value = $this->options['value'];
$this->group_info = $this->options['group_info']['default_group'];
if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
$this->options['expose']['operator_id'] = $options['expose']['operator'];
}
if ($this
->multipleExposedInput()) {
$this->group_info = array_filter($options['group_info']['default_group_multiple']);
$this->options['expose']['multiple'] = TRUE;
}
if ($this->view->display_handler
->getOption('relationships')) {
$this->definition['allow empty'] = TRUE;
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator'] = array(
'default' => '=',
);
$options['value'] = array(
'default' => '',
);
$options['group'] = array(
'default' => '1',
);
$options['exposed'] = array(
'default' => FALSE,
);
$options['expose'] = array(
'contains' => array(
'operator_id' => array(
'default' => FALSE,
),
'label' => array(
'default' => '',
),
'description' => array(
'default' => '',
),
'use_operator' => array(
'default' => FALSE,
),
'operator' => array(
'default' => '',
),
'identifier' => array(
'default' => '',
),
'required' => array(
'default' => FALSE,
),
'remember' => array(
'default' => FALSE,
),
'multiple' => array(
'default' => FALSE,
),
'remember_roles' => array(
'default' => array(
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
),
),
),
);
$options['is_grouped'] = array(
'default' => FALSE,
);
$options['group_info'] = array(
'contains' => array(
'label' => array(
'default' => '',
),
'description' => array(
'default' => '',
),
'identifier' => array(
'default' => '',
),
'optional' => array(
'default' => TRUE,
),
'widget' => array(
'default' => 'select',
),
'multiple' => array(
'default' => FALSE,
),
'remember' => array(
'default' => 0,
),
'default_group' => array(
'default' => 'All',
),
'default_group_multiple' => array(
'default' => array(),
),
'group_items' => array(
'default' => array(),
),
),
);
return $options;
}
public function adminSummary() {
return $this->operator . ' ' . $this->value;
}
public function canExpose() {
return TRUE;
}
protected function canBuildGroup() {
return $this
->isExposed() && count($this
->operatorOptions()) > 0;
}
public function isAGroup() {
return $this
->isExposed() && !empty($this->options['is_grouped']);
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if ($this
->canExpose()) {
$this
->showExposeButton($form, $form_state);
}
if ($this
->canBuildGroup()) {
$this
->showBuildGroupButton($form, $form_state);
}
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
if ($this
->isAGroup()) {
if ($this
->canBuildGroup()) {
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
$this
->showBuildGroupForm($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
}
}
else {
$this
->showOperatorForm($form, $form_state);
$this
->showValueForm($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
if ($this
->canExpose()) {
$this
->showExposeForm($form, $form_state);
}
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$this
->operatorValidate($form, $form_state);
$this
->valueValidate($form, $form_state);
if (!empty($this->options['exposed']) && !$this
->isAGroup()) {
$this
->validateExposeForm($form, $form_state);
}
if ($this
->isAGroup()) {
$this
->buildGroupValidate($form, $form_state);
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$form_state
->unsetValue('expose_button');
$form_state
->unsetValue('group_button');
if (!$this
->isAGroup()) {
$this
->operatorSubmit($form, $form_state);
$this
->valueSubmit($form, $form_state);
}
if (!empty($this->options['exposed'])) {
$this
->submitExposeForm($form, $form_state);
}
if ($this
->isAGroup()) {
$this
->buildGroupSubmit($form, $form_state);
}
}
public function showOperatorForm(&$form, FormStateInterface $form_state) {
$this
->operatorForm($form, $form_state);
$form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
$form['operator']['#suffix'] = '</div>';
}
protected function operatorForm(&$form, FormStateInterface $form_state) {
$options = $this
->operatorOptions();
if (!empty($options)) {
$form['operator'] = array(
'#type' => count($options) < 10 ? 'radios' : 'select',
'#title' => $this
->t('Operator'),
'#default_value' => $this->operator,
'#options' => $options,
);
}
}
public function operatorOptions() {
return array();
}
protected function operatorValidate($form, FormStateInterface $form_state) {
}
public function operatorSubmit($form, FormStateInterface $form_state) {
}
protected function showValueForm(&$form, FormStateInterface $form_state) {
$this
->valueForm($form, $form_state);
if (empty($this->no_operator)) {
$form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
$form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
}
}
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['value'] = array();
}
protected function valueValidate($form, FormStateInterface $form_state) {
}
protected function valueSubmit($form, FormStateInterface $form_state) {
}
public function showBuildGroupForm(&$form, FormStateInterface $form_state) {
if (empty($this->options['is_grouped'])) {
return;
}
$this
->buildExposedFiltersGroupForm($form, $form_state);
if ($form_state
->get('force_build_group_options')) {
foreach (Element::children($form['group_info']) as $id) {
if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
$form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
}
}
}
}
protected function showBuildGroupButton(&$form, FormStateInterface $form_state) {
$form['group_button'] = array(
'#prefix' => '<div class="views-grouped clearfix">',
'#suffix' => '</div>',
'#weight' => -190,
);
$grouped_description = $this
->t('Grouped filters allow a choice between predefined operator|value pairs.');
$form['group_button']['radios'] = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'js-only',
),
),
);
$form['group_button']['radios']['radios'] = array(
'#title' => $this
->t('Filter type to expose'),
'#description' => $grouped_description,
'#type' => 'radios',
'#options' => array(
$this
->t('Single filter'),
$this
->t('Grouped filters'),
),
);
if (empty($this->options['is_grouped'])) {
$form['group_button']['markup'] = array(
'#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
);
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => $this
->t('Grouped filters'),
'#submit' => array(
array(
$this,
'buildGroupForm',
),
),
'#attributes' => array(
'class' => array(
'use-ajax-submit',
),
),
);
$form['group_button']['radios']['radios']['#default_value'] = 0;
}
else {
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => $this
->t('Single filter'),
'#submit' => array(
array(
$this,
'buildGroupForm',
),
),
'#attributes' => array(
'class' => array(
'use-ajax-submit',
),
),
);
$form['group_button']['radios']['radios']['#default_value'] = 1;
}
}
public function buildGroupForm($form, FormStateInterface $form_state) {
$item =& $this->options;
$item['is_grouped'] = empty($item['is_grouped']);
if ($item['is_grouped']) {
$this
->buildGroupOptions();
}
$view = $form_state
->get('view');
$display_id = $form_state
->get('display_id');
$type = $form_state
->get('type');
$id = $form_state
->get('id');
$view
->getExecutable()
->setHandler($display_id, $type, $id, $item);
$view
->addFormToStack($form_state
->get('form_key'), $display_id, $type, $id, TRUE, TRUE);
$view
->cacheSet();
$form_state
->set('rerender', TRUE);
$form_state
->setRebuild();
$form_state
->get('force_build_group_options', TRUE);
}
public function showExposeButton(&$form, FormStateInterface $form_state) {
$form['expose_button'] = array(
'#prefix' => '<div class="views-expose clearfix">',
'#suffix' => '</div>',
'#weight' => -200,
);
$form['expose_button']['checkbox'] = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'js-only',
),
),
);
$form['expose_button']['checkbox']['checkbox'] = array(
'#title' => $this
->t('Expose this filter to visitors, to allow them to change it'),
'#type' => 'checkbox',
);
if (empty($this->options['exposed'])) {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . $this
->t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => $this
->t('Expose filter'),
'#submit' => array(
array(
$this,
'displayExposedForm',
),
),
'#attributes' => array(
'class' => array(
'use-ajax-submit',
),
),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
}
else {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . $this
->t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => $this
->t('Hide filter'),
'#submit' => array(
array(
$this,
'displayExposedForm',
),
),
'#attributes' => array(
'class' => array(
'use-ajax-submit',
),
),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
}
}
public function buildExposeForm(&$form, FormStateInterface $form_state) {
$form['#theme'] = 'views_ui_expose_filter_form';
array_unshift($form['#pre_render'], array(
get_class($this),
'preRenderFlattenData',
));
$form['expose']['#flatten'] = TRUE;
if (empty($this->always_required)) {
$form['expose']['required'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Required'),
'#default_value' => $this->options['expose']['required'],
);
}
else {
$form['expose']['required'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
$form['expose']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['label'],
'#title' => $this
->t('Label'),
'#size' => 40,
);
$form['expose']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['description'],
'#title' => $this
->t('Description'),
'#size' => 60,
);
if (!empty($form['operator']['#type'])) {
$form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
$form['operator']['#suffix'] = '</div>';
$form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
$form['value']['#suffix'] = '</div>';
$form['expose']['use_operator'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Expose operator'),
'#description' => $this
->t('Allow the user to choose the operator.'),
'#default_value' => !empty($this->options['expose']['use_operator']),
);
$form['expose']['operator_id'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['operator_id'],
'#title' => $this
->t('Operator identifier'),
'#size' => 40,
'#description' => $this
->t('This will appear in the URL after the ? to identify this operator.'),
'#states' => array(
'visible' => array(
':input[name="options[expose][use_operator]"]' => array(
'checked' => TRUE,
),
),
),
);
}
else {
$form['expose']['operator_id'] = array(
'#type' => 'value',
'#value' => '',
);
}
if (empty($this->alwaysMultiple)) {
$form['expose']['multiple'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Allow multiple selections'),
'#description' => $this
->t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['expose']['multiple'],
);
}
$form['expose']['remember'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Remember the last selection'),
'#description' => $this
->t('Enable to remember the last selection made by the user.'),
'#default_value' => $this->options['expose']['remember'],
);
$role_options = array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names());
$form['expose']['remember_roles'] = array(
'#type' => 'checkboxes',
'#title' => $this
->t('User roles'),
'#description' => $this
->t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
'#default_value' => $this->options['expose']['remember_roles'],
'#options' => $role_options,
'#states' => array(
'invisible' => array(
':input[name="options[expose][remember]"]' => array(
'checked' => FALSE,
),
),
),
);
$form['expose']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['identifier'],
'#title' => $this
->t('Filter identifier'),
'#size' => 40,
'#description' => $this
->t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
);
}
public function validateExposeForm($form, FormStateInterface $form_state) {
$identifier = $form_state
->getValue(array(
'options',
'expose',
'identifier',
));
if (empty($identifier)) {
$form_state
->setError($form['expose']['identifier'], $this
->t('The identifier is required if the filter is exposed.'));
}
elseif ($identifier == 'value') {
$form_state
->setError($form['expose']['identifier'], $this
->t('This identifier is not allowed.'));
}
if (!$this->view->display_handler
->isIdentifierUnique($form_state
->get('id'), $identifier)) {
$form_state
->setError($form['expose']['identifier'], $this
->t('This identifier is used by another handler.'));
}
}
protected function buildGroupValidate($form, FormStateInterface $form_state) {
if (!$form_state
->isValueEmpty(array(
'options',
'group_info',
))) {
$identifier = $form_state
->getValue(array(
'options',
'group_info',
'identifier',
));
if (empty($identifier)) {
$form_state
->setError($form['group_info']['identifier'], $this
->t('The identifier is required if the filter is exposed.'));
}
elseif ($identifier == 'value') {
$form_state
->setError($form['group_info']['identifier'], $this
->t('This identifier is not allowed.'));
}
if (!$this->view->display_handler
->isIdentifierUnique($form_state
->get('id'), $identifier)) {
$form_state
->setError($form['group_info']['identifier'], $this
->t('This identifier is used by another handler.'));
}
}
if ($group_items = $form_state
->getValue(array(
'options',
'group_info',
'group_items',
))) {
$operators = $this
->operators();
foreach ($group_items as $id => $group) {
if (empty($group['remove'])) {
if (!empty($group['title']) && $operators[$group['operator']]['values'] > 0) {
if (!is_array($group['value']) && trim($group['value']) == "" || is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) == 0) {
$form_state
->setError($form['group_info']['group_items'][$id]['value'], $this
->t('The value is required if title for this item is defined.'));
}
}
if (!is_array($group['value']) && trim($group['value']) != "" || is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) > 0) {
if (empty($group['title'])) {
$form_state
->setError($form['group_info']['group_items'][$id]['title'], $this
->t('The title is required if value for this item is defined.'));
}
}
}
}
}
}
protected function buildGroupSubmit($form, FormStateInterface $form_state) {
$groups = array();
$group_items = $form_state
->getValue(array(
'options',
'group_info',
'group_items',
));
uasort($group_items, array(
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
));
$new_id = 1;
$new_default = 'All';
foreach ($group_items as $id => $group) {
if (empty($group['remove'])) {
unset($group['remove']);
unset($group['weight']);
$groups[$new_id] = $group;
if ($form_state
->getValue(array(
'options',
'group_info',
'default_group',
)) == $id) {
$new_default = $new_id;
}
}
$new_id++;
}
if ($new_default != 'All') {
$form_state
->setValue(array(
'options',
'group_info',
'default_group',
), $new_default);
}
$filter_default_multiple = $form_state
->getValue(array(
'options',
'group_info',
'default_group_multiple',
));
$form_state
->setValue(array(
'options',
'group_info',
'default_group_multiple',
), array_filter($filter_default_multiple));
$form_state
->setValue(array(
'options',
'group_info',
'group_items',
), $groups);
}
public function defaultExposeOptions() {
$this->options['expose'] = array(
'use_operator' => FALSE,
'operator' => $this->options['id'] . '_op',
'identifier' => $this->options['id'],
'label' => $this->definition['title'],
'description' => NULL,
'remember' => FALSE,
'multiple' => FALSE,
'required' => FALSE,
);
}
protected function buildGroupOptions() {
$this->options['group_info'] = array(
'label' => $this->definition['title'],
'description' => NULL,
'identifier' => $this->options['id'],
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => array(),
'group_items' => array(),
);
}
public function groupForm(&$form, FormStateInterface $form_state) {
if (!empty($this->options['group_info']['optional']) && !$this
->multipleExposedInput()) {
$groups = array(
'All' => $this
->t('- Any -'),
);
}
foreach ($this->options['group_info']['group_items'] as $id => $group) {
if (!empty($group['title'])) {
$groups[$id] = $id != 'All' ? $this
->t($group['title']) : $group['title'];
}
}
if (count($groups)) {
$value = $this->options['group_info']['identifier'];
$form[$value] = array(
'#title' => $this->options['group_info']['label'],
'#type' => $this->options['group_info']['widget'],
'#default_value' => $this->group_info,
'#options' => $groups,
);
if (!empty($this->options['group_info']['multiple'])) {
if (count($groups) < 5) {
$form[$value]['#type'] = 'checkboxes';
}
else {
$form[$value]['#type'] = 'select';
$form[$value]['#size'] = 5;
$form[$value]['#multiple'] = TRUE;
}
unset($form[$value]['#default_value']);
$user_input = $form_state
->getUserInput();
if (empty($user_input)) {
$user_input[$value] = $this->group_info;
$form_state
->setUserInput($user_input);
}
}
$this->options['expose']['label'] = '';
}
}
public function buildExposedForm(&$form, FormStateInterface $form_state) {
if (empty($this->options['exposed'])) {
return;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
$operator = $this->options['expose']['operator_id'];
$this
->operatorForm($form, $form_state);
$form[$operator] = $form['operator'];
$this
->exposedTranslate($form[$operator], 'operator');
unset($form['operator']);
}
if (!empty($this->options['expose']['identifier'])) {
$value = $this->options['expose']['identifier'];
$this
->valueForm($form, $form_state);
$form[$value] = $form['value'];
if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
unset($form[$value]['#title']);
}
$this
->exposedTranslate($form[$value], 'value');
if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || $form['#type'] == 'select' && !empty($form['#multiple']))) {
unset($form[$value]['#default_value']);
}
if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
$form[$value]['#default_value'] = 'All';
}
if ($value != 'value') {
unset($form['value']);
}
}
}
protected function buildExposedFiltersGroupForm(&$form, FormStateInterface $form_state) {
if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
return;
}
$form['#theme'] = 'views_ui_build_group_filter_form';
array_unshift($form['#pre_render'], array(
get_class($this),
'preRenderFlattenData',
));
$form['group_info']['#flatten'] = TRUE;
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => $this
->t('Filter identifier'),
'#size' => 40,
'#description' => $this
->t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => $this
->t('Label'),
'#size' => 40,
);
$form['group_info']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['description'],
'#title' => $this
->t('Description'),
'#size' => 60,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Optional'),
'#description' => $this
->t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['multiple'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Allow multiple selections'),
'#description' => $this
->t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['group_info']['multiple'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => $this
->t('Widget type'),
'#options' => array(
'radios' => $this
->t('Radios'),
'select' => $this
->t('Select'),
),
'#description' => $this
->t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Remember'),
'#description' => $this
->t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => $this
->t('Filter identifier'),
'#size' => 40,
'#description' => $this
->t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => $this
->t('Label'),
'#size' => 40,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Optional'),
'#description' => $this
->t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => $this
->t('Widget type'),
'#options' => array(
'radios' => $this
->t('Radios'),
'select' => $this
->t('Select'),
),
'#description' => $this
->t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Remember'),
'#description' => $this
->t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
$groups = array(
'All' => $this
->t('- Any -'),
);
if (count($this->options['group_info']['group_items']) == 0) {
$this->options['group_info']['group_items'] = array_fill(1, 3, array());
}
$default_weight = 0;
foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
if (!$form_state
->isValueEmpty(array(
'options',
'group_info',
'group_items',
$item_id,
'remove',
))) {
continue;
}
$row = array();
$groups[$item_id] = $this
->t('Grouping @id', array(
'@id' => $item_id,
));
$this
->operatorForm($row, $form_state);
$row['operator']['#type'] = 'select';
$row['operator']['#title'] = '';
$this
->valueForm($row, $form_state);
FormHelper::rewriteStatesSelector($row['value'], ':input[name="options[operator]"]', ':input[name="options[group_info][group_items][' . $item_id . '][operator]"]');
$children = Element::children($row['value']);
if (!empty($children)) {
foreach ($children as $child) {
foreach ($row['value'][$child]['#states']['visible'] as $state) {
if (isset($state[':input[name="options[group_info][group_items][' . $item_id . '][operator]"]'])) {
$row['value'][$child]['#title'] = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$child])) {
$row['value'][$child]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$child];
}
break;
}
}
}
}
else {
if (isset($this->options['group_info']['group_items'][$item_id]['value']) && $this->options['group_info']['group_items'][$item_id]['value'] != '') {
$row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
}
}
if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
$row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
}
$default_title = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
$default_title = $this->options['group_info']['group_items'][$item_id]['title'];
}
$form['group_info']['group_items'][$item_id] = array(
'title' => array(
'#title' => $this
->t('Label'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $default_title,
),
'operator' => $row['operator'],
'value' => $row['value'],
'remove' => array(
'#type' => 'checkbox',
'#id' => 'views-removed-' . $item_id,
'#attributes' => array(
'class' => array(
'views-remove-checkbox',
),
),
'#default_value' => 0,
),
'weight' => array(
'#title' => $this
->t('Weight'),
'#title_display' => 'invisible',
'#type' => 'weight',
'#delta' => 10,
'#default_value' => $default_weight++,
'#attributes' => array(
'class' => array(
'weight',
),
),
),
);
}
$form['group_info']['default_group'] = array(
'#type' => 'radios',
'#options' => $groups,
'#default_value' => $this->options['group_info']['default_group'],
'#required' => TRUE,
'#attributes' => array(
'class' => array(
'default-radios',
),
),
);
$form['group_info']['default_group_multiple'] = array(
'#type' => 'checkboxes',
'#options' => $groups,
'#default_value' => $this->options['group_info']['default_group_multiple'],
'#attributes' => array(
'class' => array(
'default-checkboxes',
),
),
);
$form['group_info']['add_group'] = array(
'#prefix' => '<div class="views-build-group clear-block">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => $this
->t('Add another item'),
'#submit' => array(
array(
$this,
'addGroupForm',
),
),
'#attributes' => array(
'class' => array(
'use-ajax-submit',
),
),
);
$js = array();
$js['tableDrag']['views-filter-groups']['weight'][0] = array(
'target' => 'weight',
'source' => NULL,
'relationship' => 'sibling',
'action' => 'order',
'hidden' => TRUE,
'limit' => 0,
);
$js_settings = $form_state
->get('js_settings');
if ($js_settings && is_array($js)) {
$js_settings = array_merge($js_settings, $js);
}
else {
$js_settings = $js;
}
$form_state
->set('js_settings', $js_settings);
}
public function addGroupForm($form, FormStateInterface $form_state) {
$item =& $this->options;
$item['group_info']['group_items'][] = array();
$view = $form_state
->get('view');
$display_id = $form_state
->get('display_id');
$type = $form_state
->get('type');
$id = $form_state
->get('id');
$view
->getExecutable()
->setHandler($display_id, $type, $id, $item);
$view
->cacheSet();
$form_state
->set('rerender', TRUE);
$form_state
->setRebuild();
$form_state
->get('force_build_group_options', TRUE);
}
protected function exposedTranslate(&$form, $type) {
if (!isset($form['#type'])) {
return;
}
if ($form['#type'] == 'radios') {
$form['#type'] = 'select';
}
if ($form['#type'] == 'checkboxes') {
if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
$form['#type'] = 'select';
}
if (!empty($this->options['expose']['multiple'])) {
$form['#multiple'] = TRUE;
}
}
if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
unset($form['#multiple']);
$form['#size'] = NULL;
}
if ($form['#type'] == 'select') {
$this
->prepareFilterSelectOptions($form['#options']);
}
if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
$form['#options'] = array(
'All' => $this
->t('- Any -'),
) + $form['#options'];
$form['#default_value'] = 'All';
}
if (!empty($this->options['expose']['required'])) {
$form['#required'] = TRUE;
}
}
protected function prepareFilterSelectOptions(&$options) {
foreach ($options as $value => $label) {
if (is_array($label)) {
$this
->prepareFilterSelectOptions($options[$value]);
}
elseif (is_object($label) && isset($label->option)) {
$this
->prepareFilterSelectOptions($options[$value]->option);
}
else {
$options[$value] = strip_tags(Html::decodeEntities((string) $label));
}
}
}
public function exposedInfo() {
if (empty($this->options['exposed'])) {
return;
}
if ($this
->isAGroup()) {
return array(
'value' => $this->options['group_info']['identifier'],
'label' => $this->options['group_info']['label'],
'description' => $this->options['group_info']['description'],
);
}
return array(
'operator' => $this->options['expose']['operator_id'],
'value' => $this->options['expose']['identifier'],
'label' => $this->options['expose']['label'],
'description' => $this->options['expose']['description'],
);
}
public function convertExposedInput(&$input, $selected_group_id = NULL) {
if ($this
->isAGroup()) {
if (!empty($selected_group_id)) {
$selected_group = $selected_group_id;
}
else {
$selected_group = $input[$this->options['group_info']['identifier']];
}
if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
return NULL;
}
if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
return FALSE;
}
if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
$input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
if (isset($this->options['group_info']['group_items'][$selected_group]['value']) && $this->options['group_info']['group_items'][$selected_group]['value'] != '') {
$input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
}
$this->options['expose']['use_operator'] = TRUE;
$this->group_info = $input[$this->options['group_info']['identifier']];
return TRUE;
}
else {
return FALSE;
}
}
}
public function groupMultipleExposedInput(&$input) {
if (!empty($input[$this->options['group_info']['identifier']])) {
return array_filter($input[$this->options['group_info']['identifier']]);
}
return array();
}
public function multipleExposedInput() {
return $this
->isAGroup() && !empty($this->options['group_info']['multiple']);
}
public function storeGroupInput($input, $status) {
if (!$this
->isAGroup() || empty($this->options['group_info']['identifier'])) {
return TRUE;
}
if (empty($this->options['group_info']['remember'])) {
return;
}
$display_id = $this->view->display_handler
->isDefaulted('filters') ? 'default' : $this->view->current_display;
if ($status === FALSE && isset($_SESSION['views'][$this->view->storage
->id()][$display_id])) {
$session =& $_SESSION['views'][$this->view->storage
->id()][$display_id];
if (isset($session[$this->options['group_info']['identifier']])) {
unset($session[$this->options['group_info']['identifier']]);
}
}
if ($status !== FALSE) {
if (!isset($_SESSION['views'][$this->view->storage
->id()][$display_id])) {
$_SESSION['views'][$this->view->storage
->id()][$display_id] = array();
}
$session =& $_SESSION['views'][$this->view->storage
->id()][$display_id];
$session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
}
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
$this->operator = $input[$this->options['expose']['operator_id']];
}
if (!empty($this->options['expose']['identifier'])) {
$value = $input[$this->options['expose']['identifier']];
if (empty($this->options['expose']['required'])) {
if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
$value = ' ';
}
if ($this->operator != 'empty' && $this->operator != 'not empty') {
if ($value == 'All' || $value === array()) {
return FALSE;
}
}
if (!empty($this->alwaysMultiple) && $value === '') {
return FALSE;
}
}
if (isset($value)) {
$this->value = $value;
if (empty($this->alwaysMultiple) && empty($this->options['expose']['multiple']) && !is_array($value)) {
$this->value = array(
$value,
);
}
}
else {
return FALSE;
}
}
return TRUE;
}
public function storeExposedInput($input, $status) {
if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
return TRUE;
}
if (empty($this->options['expose']['remember'])) {
return;
}
$user = \Drupal::currentUser();
$allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
$intersect_rids = array_intersect(array_keys($allowed_rids), $user
->getRoles());
if (empty($intersect_rids)) {
return;
}
$display_id = $this->view->display_handler
->isDefaulted('filters') ? 'default' : $this->view->current_display;
$operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
if (!$status && isset($_SESSION['views'][$this->view->storage
->id()][$display_id])) {
$session =& $_SESSION['views'][$this->view->storage
->id()][$display_id];
if ($operator && isset($session[$this->options['expose']['operator_id']])) {
unset($session[$this->options['expose']['operator_id']]);
}
if (isset($session[$this->options['expose']['identifier']])) {
unset($session[$this->options['expose']['identifier']]);
}
}
if ($status) {
if (!isset($_SESSION['views'][$this->view->storage
->id()][$display_id])) {
$_SESSION['views'][$this->view->storage
->id()][$display_id] = array();
}
$session =& $_SESSION['views'][$this->view->storage
->id()][$display_id];
if ($operator && isset($input[$this->options['expose']['operator_id']])) {
$session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
}
$session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
}
}
public function query() {
$this
->ensureMyTable();
$this->query
->addWhere($this->options['group'], "{$this->tableAlias}.{$this->realField}", $this->value, $this->operator);
}
public function canGroup() {
return TRUE;
}
protected static function arrayFilterZero($var) {
return trim($var) != '';
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
$cache_contexts = [];
if ($this
->isExposed()) {
$cache_contexts[] = 'url';
}
return $cache_contexts;
}
public function getCacheTags() {
return [];
}
}