Newer
Older
Angie Byron
committed
* Contains \Drupal\views\Plugin\views\filter\FilterPluginBase.
namespace Drupal\views\Plugin\views\filter;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\Core\Render\Element;
Tim Plunkett
committed
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\Component\Utility\String as UtilityString;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
Tim Plunkett
committed
use Drupal\views\ViewExecutable;
Angie Byron
committed
* @defgroup views_filter_handlers Views filter handler plugins
Angie Byron
committed
* Plugins that handle views filtering.
Angie Byron
committed
* Filter handler plugins extend
Alex Pott
committed
* \Drupal\views\Plugin\views\filter\FilterPluginBase. They must be annotated
Angie Byron
committed
* with \Drupal\views\Annotation\ViewsFilter annotation, and they must be in
* namespace directory Plugin\views\filter.
*
* The following items can go into a hook_views_data() implementation in a
* filter section to affect how the filter handler will behave:
* - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
* available as standard operators.
*
Angie Byron
committed
* You can refine the behavior of filters by setting the following Boolean
* member variables to TRUE in your plugin class:
* - $alwaysMultiple: Disable the possibility of forcing a single value.
* - $no_operator: Disable the possibility of using operators.
* - $always_required: Disable the possibility of allowing an exposed input to
* be optional.
Angie Byron
committed
* @ingroup views_plugins
* @see plugin_api
Angie Byron
committed
* Base class for Views filters handler plugins.
Tim Plunkett
committed
abstract class FilterPluginBase extends HandlerBase {
/**
* Contains the actual value of the field,either configured in the views ui
* or entered in the exposed filters.
*/
var $value = NULL;
/**
* Contains the operator which is used on the query.
*/
var $operator = '=';
Tim Plunkett
committed
/**
* Contains the information of the selected item in a gruped filter.
*/
var $group_info = NULL;
/**
* @var bool
* Disable the possibility to force a single value.
*/
Alex Pott
committed
protected $alwaysMultiple = FALSE;
/**
* @var bool
* Disable the possibility to use operators.
*/
var $no_operator = FALSE;
/**
* @var bool
* Disable the possibility to allow a exposed input to be optional.
*/
var $always_required = FALSE;
/**
* Overrides \Drupal\views\Plugin\views\HandlerBase::init().
*
* Provide some extra help to get the operator/value easier to use.
*
* This likely has to be overridden by filters which are more complex
* than simple operator/value.
*/
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'];
Tim Plunkett
committed
$this->group_info = $this->options['group_info']['default_group'];
Tim Plunkett
committed
// Set the default value of the operator ID.
if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
Tim Plunkett
committed
$this->options['expose']['operator_id'] = $options['expose']['operator'];
if ($this->multipleExposedInput()) {
Tim Plunkett
committed
$this->group_info = array_filter($options['group_info']['default_group_multiple']);
$this->options['expose']['multiple'] = TRUE;
}
Tim Plunkett
committed
// If there are relationships in the view, allow empty should be true
// so that we can do IS NULL checks on items. Not all filters respect
// allow empty, but string and numeric do and that covers enough.
Daniel Wehner
committed
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, 'bool' => TRUE);
$options['expose'] = array(
'contains' => array(
'operator_id' => array('default' => FALSE),
'label' => array('default' => '', 'translatable' => TRUE),
Damian Lee
committed
'description' => array('default' => '', 'translatable' => TRUE),
'use_operator' => array('default' => FALSE, 'bool' => TRUE),
'operator' => array('default' => ''),
'identifier' => array('default' => ''),
'required' => array('default' => FALSE, 'bool' => TRUE),
'remember' => array('default' => FALSE, 'bool' => TRUE),
'multiple' => array('default' => FALSE, 'bool' => TRUE),
Janez Urevc
committed
'remember_roles' => array('default' => array(
DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
)),
Tim Plunkett
committed
// A group is a combination of a filter, an operator and a value
// operating like a single filter.
// Users can choose from a select box which group they want to apply.
// Views will filter the view according to the defined values.
// Because it acts as a standard filter, we have to define
// an identifier and other settings like the widget and the label.
// This settings are saved in another array to allow users to switch
// between a normal filter and a group of filters with a single click.
$options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
$options['group_info'] = array(
'contains' => array(
'label' => array('default' => '', 'translatable' => TRUE),
Damian Lee
committed
'description' => array('default' => '', 'translatable' => TRUE),
Tim Plunkett
committed
'identifier' => array('default' => ''),
'optional' => array('default' => TRUE, 'bool' => TRUE),
'widget' => array('default' => 'select'),
'multiple' => array('default' => FALSE, 'bool' => TRUE),
'remember' => array('default' => 0),
'default_group' => array('default' => 'All'),
'default_group_multiple' => array('default' => array()),
'group_items' => array('default' => array()),
),
);
return $options;
}
/**
* Display the filter on the administrative summary
*/
public function adminSummary() {
return UtilityString::checkPlain((string) $this->operator) . ' ' . UtilityString::checkPlain((string) $this->value);
}
/**
* Determine if a filter can be exposed.
*/
public function canExpose() { return TRUE; }
Tim Plunkett
committed
/**
* Determine if a filter can be converted into a group.
* Only exposed filters with operators available can be converted into groups.
*/
protected function canBuildGroup() {
Alex Pott
committed
return $this->isExposed() && (count($this->operatorOptions()) > 0);
Tim Plunkett
committed
}
/**
* Returns TRUE if the exposed filter works like a grouped filter.
*/
public function isAGroup() {
return $this->isExposed() && !empty($this->options['is_grouped']);
Tim Plunkett
committed
}
/**
* Provide the basic form which calls through to subforms.
* If overridden, it is best to call through to the parent,
* or to at least make sure all of the functions in this form
* are called.
*/
Dries Buytaert
committed
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if ($this->canExpose()) {
$this->showExposeButton($form, $form_state);
if ($this->canBuildGroup()) {
Alex Pott
committed
$this->showBuildGroupButton($form, $form_state);
Tim Plunkett
committed
}
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
if ($this->isAGroup()) {
if ($this->canBuildGroup()) {
Tim Plunkett
committed
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
// Render the build group form.
Alex Pott
committed
$this->showBuildGroupForm($form, $form_state);
Tim Plunkett
committed
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
}
}
else {
Alex Pott
committed
// Add the subform from operatorForm().
Alex Pott
committed
$this->showOperatorForm($form, $form_state);
// Add the subform from valueForm().
Alex Pott
committed
$this->showValueForm($form, $form_state);
Tim Plunkett
committed
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
if ($this->canExpose()) {
// Add the subform from buildExposeForm().
$this->showExposeForm($form, $form_state);
Tim Plunkett
committed
}
}
}
/**
* Simple validate handler
*/
Dries Buytaert
committed
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$this->operatorValidate($form, $form_state);
Alex Pott
committed
$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);
Tim Plunkett
committed
}
}
/**
* Simple submit handler
*/
Dries Buytaert
committed
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
Alex Pott
committed
// Do not store these values.
$form_state->unsetValue('expose_button');
$form_state->unsetValue('group_button');
if (!$this->isAGroup()) {
Alex Pott
committed
$this->operatorSubmit($form, $form_state);
$this->valueSubmit($form, $form_state);
Tim Plunkett
committed
}
$this->submitExposeForm($form, $form_state);
if ($this->isAGroup()) {
$this->buildGroupSubmit($form, $form_state);
Tim Plunkett
committed
}
}
/**
* Shortcut to display the operator form.
*/
Dries Buytaert
committed
public function showOperatorForm(&$form, FormStateInterface $form_state) {
Alex Pott
committed
$this->operatorForm($form, $form_state);
$form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
$form['operator']['#suffix'] = '</div>';
}
/**
* Options form subform for setting the operator.
*
* This may be overridden by child classes, and it must
* define $form['operator'];
*
* @see buildOptionsForm()
Dries Buytaert
committed
protected function operatorForm(&$form, FormStateInterface $form_state) {
Alex Pott
committed
$options = $this->operatorOptions();
if (!empty($options)) {
$form['operator'] = array(
'#type' => count($options) < 10 ? 'radios' : 'select',
'#title' => t('Operator'),
'#default_value' => $this->operator,
'#options' => $options,
);
}
}
/**
* Provide a list of options for the default operator form.
Alex Pott
committed
* Should be overridden by classes that don't override operatorForm
Alex Pott
committed
public function operatorOptions() { return array(); }
/**
* Validate the operator form.
*/
Dries Buytaert
committed
protected function operatorValidate($form, FormStateInterface $form_state) { }
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
Dries Buytaert
committed
public function operatorSubmit($form, FormStateInterface $form_state) { }
/**
* Shortcut to display the value form.
*/
Dries Buytaert
committed
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>';
}
}
/**
* Options form subform for setting options.
*
* This should be overridden by all child classes and it must
* define $form['value']
*
* @see buildOptionsForm()
Dries Buytaert
committed
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['value'] = array();
}
/**
* Validate the options form.
*/
Dries Buytaert
committed
protected function valueValidate($form, FormStateInterface $form_state) { }
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
Dries Buytaert
committed
protected function valueSubmit($form, FormStateInterface $form_state) { }
Tim Plunkett
committed
/**
* Shortcut to display the exposed options form.
*/
Dries Buytaert
committed
public function showBuildGroupForm(&$form, FormStateInterface $form_state) {
Tim Plunkett
committed
if (empty($this->options['is_grouped'])) {
return;
}
Angie Byron
committed
$this->buildExposedFiltersGroupForm($form, $form_state);
Tim Plunkett
committed
// When we click the expose button, we add new gadgets to the form but they
Angie Byron
committed
// have no data in POST so their defaults get wiped out. This prevents
Tim Plunkett
committed
// these defaults from getting wiped out. This setting will only be TRUE
// during a 2nd pass rerender.
if (!empty($form_state['force_build_group_options'])) {
Angie Byron
committed
foreach (Element::children($form['group_info']) as $id) {
Tim Plunkett
committed
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'];
}
}
}
}
/**
* Shortcut to display the build_group/hide button.
*/
Dries Buytaert
committed
protected function showBuildGroupButton(&$form, FormStateInterface $form_state) {
Tim Plunkett
committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
$form['group_button'] = array(
'#prefix' => '<div class="views-grouped clearfix">',
'#suffix' => '</div>',
// Should always come after the description and the relationship.
'#weight' => -190,
);
$grouped_description = 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' => t('Filter type to expose'),
'#description' => $grouped_description,
'#type' => 'radios',
'#options' => array(
t('Single filter'),
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' => t('Grouped filters'),
Angie Byron
committed
'#submit' => array(array($this, 'buildGroupForm')),
Angie Byron
committed
'#attributes' => array('class' => array('use-ajax-submit')),
Tim Plunkett
committed
);
$form['group_button']['radios']['radios']['#default_value'] = 0;
}
else {
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Single filter'),
Angie Byron
committed
'#submit' => array(array($this, 'buildGroupForm')),
Angie Byron
committed
'#attributes' => array('class' => array('use-ajax-submit')),
Tim Plunkett
committed
);
$form['group_button']['radios']['radios']['#default_value'] = 1;
}
}
Angie Byron
committed
/**
* Displays the Build Group form.
*/
Dries Buytaert
committed
public function buildGroupForm($form, FormStateInterface $form_state) {
Angie Byron
committed
$item = &$this->options;
// flip. If the filter was a group, set back to a standard filter.
$item['is_grouped'] = empty($item['is_grouped']);
// If necessary, set new defaults:
if ($item['is_grouped']) {
$this->buildGroupOptions();
Angie Byron
committed
}
Angie Byron
committed
$form_state['view']->getExecutable()->setHandler($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
Angie Byron
committed
$form_state['view']->addFormToStack($form_state['form_key'], $form_state['display_id'], $form_state['type'], $form_state['id'], TRUE, TRUE);
Alex Pott
committed
$form_state['view']->cacheSet();
Angie Byron
committed
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
$form_state['force_build_group_options'] = TRUE;
}
/**
* Shortcut to display the expose/hide button.
*/
Dries Buytaert
committed
public function showExposeButton(&$form, FormStateInterface $form_state) {
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
$form['expose_button'] = array(
'#prefix' => '<div class="views-expose clearfix">',
'#suffix' => '</div>',
// Should always come after the description and the relationship.
'#weight' => -200,
);
// Add a checkbox for JS users, which will have behavior attached to it
// so it can replace the button.
$form['expose_button']['checkbox'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array('class' => array('js-only')),
);
$form['expose_button']['checkbox']['checkbox'] = array(
'#title' => t('Expose this filter to visitors, to allow them to change it'),
'#type' => 'checkbox',
);
// Then add the button itself.
if (empty($this->options['exposed'])) {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . 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' => t('Expose filter'),
Angie Byron
committed
'#submit' => array(array($this, 'displayExposedForm')),
Angie Byron
committed
'#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">' . 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' => t('Hide filter'),
Angie Byron
committed
'#submit' => array(array($this, 'displayExposedForm')),
Angie Byron
committed
'#attributes' => array('class' => array('use-ajax-submit')),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
}
}
/**
* Options form subform for exposed filter options.
*
* @see buildOptionsForm()
Dries Buytaert
committed
public function buildExposeForm(&$form, FormStateInterface $form_state) {
$form['#theme'] = 'views_ui_expose_filter_form';
// #flatten will move everything from $form['expose'][$key] to $form[$key]
// prior to rendering. That's why the preRender for it needs to run first,
// so that when the next preRender (the one for fieldsets) runs, it gets
Alex Pott
committed
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' => 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' => t('Label'),
'#size' => 40,
);
Damian Lee
committed
$form['expose']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['description'],
'#title' => t('Description'),
'#size' => 60,
);
if (!empty($form['operator']['#type'])) {
// Increase the width of the left (operator) column.
$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' => t('Expose operator'),
'#description' => 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' => t('Operator identifier'),
'#size' => 40,
'#description' => 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' => '',
);
}
Alex Pott
committed
if (empty($this->alwaysMultiple)) {
$form['expose']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple selections'),
'#description' => t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['expose']['multiple'],
);
}
$form['expose']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember the last selection'),
'#description' => t('Enable to remember the last selection made by the user.'),
'#default_value' => $this->options['expose']['remember'],
);
$role_options = array_map('\Drupal\Component\Utility\String::checkPlain', user_role_names());
Janez Urevc
committed
$form['expose']['remember_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles'),
'#description' => 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' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
);
}
/**
* Validate the options form.
*/
Dries Buytaert
committed
public function validateExposeForm($form, FormStateInterface $form_state) {
Alex Pott
committed
$identifier = $form_state->getValue(array('options', 'expose', 'identifier'));
if (empty($identifier)) {
Angie Byron
committed
form_error($form['expose']['identifier'], $form_state, t('The identifier is required if the filter is exposed.'));
Alex Pott
committed
elseif ($identifier == 'value') {
Angie Byron
committed
form_error($form['expose']['identifier'], $form_state, t('This identifier is not allowed.'));
Alex Pott
committed
if (!$this->view->display_handler->isIdentifierUnique($form_state['id'], $identifier)) {
Angie Byron
committed
form_error($form['expose']['identifier'], $form_state, t('This identifier is used by another handler.'));
Dries Buytaert
committed
/**
Tim Plunkett
committed
* Validate the build group options form.
*/
Dries Buytaert
committed
protected function buildGroupValidate($form, FormStateInterface $form_state) {
Alex Pott
committed
if (!$form_state->isValueEmpty(array('options', 'group_info'))) {
$identifier = $form_state->getValue(array('options', 'group_info', 'identifier'));
if (empty($identifier)) {
Angie Byron
committed
form_error($form['group_info']['identifier'], $form_state, t('The identifier is required if the filter is exposed.'));
Tim Plunkett
committed
}
Tim Plunkett
committed
Alex Pott
committed
elseif ($identifier == 'value') {
Angie Byron
committed
form_error($form['group_info']['identifier'], $form_state, t('This identifier is not allowed.'));
Tim Plunkett
committed
}
Tim Plunkett
committed
Alex Pott
committed
if (!$this->view->display_handler->isIdentifierUnique($form_state['id'], $identifier)) {
Angie Byron
committed
form_error($form['group_info']['identifier'], $form_state, t('This identifier is used by another handler.'));
Tim Plunkett
committed
}
Tim Plunkett
committed
}
Alex Pott
committed
if ($group_items = $form_state->getValue(array('options', 'group_info', 'group_items'))) {
$operators = $this->operators();
Alex Pott
committed
foreach ($group_items as $id => $group) {
Tim Plunkett
committed
if (empty($group['remove'])) {
// Check if the title is defined but value wasn't defined.
if (!empty($group['title']) && $operators[$group['operator']]['values'] > 0) {
Tim Plunkett
committed
if ((!is_array($group['value']) && trim($group['value']) == "") ||
catch
committed
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) == 0)) {
Angie Byron
committed
form_error($form['group_info']['group_items'][$id]['value'], $form_state,
Tim Plunkett
committed
t('The value is required if title for this item is defined.'));
}
}
// Check if the value is defined but title wasn't defined.
if ((!is_array($group['value']) && trim($group['value']) != "") ||
catch
committed
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) > 0)) {
Tim Plunkett
committed
if (empty($group['title'])) {
Angie Byron
committed
form_error($form['group_info']['group_items'][$id]['title'], $form_state,
Tim Plunkett
committed
t('The title is required if value for this item is defined.'));
}
}
}
}
}
}
/**
* Save new group items, re-enumerates and remove groups marked to delete.
*/
Dries Buytaert
committed
protected function buildGroupSubmit($form, FormStateInterface $form_state) {
Tim Plunkett
committed
$groups = array();
Alex Pott
committed
$group_items = $form_state->getValue(array('options', 'group_info', 'group_items'));
uasort($group_items, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement'));
Tim Plunkett
committed
// Filter out removed items.
// Start from 1 to avoid problems with #default_value in the widget.
$new_id = 1;
$new_default = 'All';
Alex Pott
committed
foreach ($group_items as $id => $group) {
Tim Plunkett
committed
if (empty($group['remove'])) {
// Don't store this.
unset($group['remove']);
unset($group['weight']);
$groups[$new_id] = $group;
Alex Pott
committed
if ($form_state->getValue(array('options', 'group_info', 'default_group')) == $id) {
Tim Plunkett
committed
$new_default = $new_id;
}
}
$new_id++;
}
if ($new_default != 'All') {
Alex Pott
committed
$form_state->setValue(array('options', 'group_info', 'default_group'), $new_default);
Tim Plunkett
committed
}
Alex Pott
committed
$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));
Tim Plunkett
committed
Alex Pott
committed
$form_state->setValue(array('options', 'group_info', 'group_items'), $groups);
Tim Plunkett
committed
}
/**
* Provide default options for exposed filters.
*/
public function defaultExposeOptions() {
$this->options['expose'] = array(
'use_operator' => FALSE,
'operator' => $this->options['id'] . '_op',
'identifier' => $this->options['id'],
'label' => $this->definition['title'],
Damian Lee
committed
'description' => NULL,
'remember' => FALSE,
'multiple' => FALSE,
'required' => FALSE,
);
}
Dries Buytaert
committed
/**
Tim Plunkett
committed
* Provide default options for exposed filters.
*/
protected function buildGroupOptions() {
Tim Plunkett
committed
$this->options['group_info'] = array(
'label' => $this->definition['title'],
Damian Lee
committed
'description' => NULL,
Tim Plunkett
committed
'identifier' => $this->options['id'],
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => array(),
'group_items' => array(),
);
}
/**
* Build a form containing a group of operator | values to apply as a
* single filter.
*/
Dries Buytaert
committed
public function groupForm(&$form, FormStateInterface $form_state) {
if (!empty($this->options['group_info']['optional']) && !$this->multipleExposedInput()) {
$groups = array('All' => t('- Any -'));
Tim Plunkett
committed
}
foreach ($this->options['group_info']['group_items'] as $id => $group) {
if (!empty($group['title'])) {
$groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
}
}
if (count($groups)) {
$value = $this->options['group_info']['identifier'];
$form[$value] = array(
'#title' => UtilityString::checkPlain($this->options['group_info']['label']),
Tim Plunkett
committed
'#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']);
Angie Byron
committed
$user_input = $form_state->getUserInput();
if (empty($user_input)) {
$user_input[$value] = $this->group_info;
$form_state->setUserInput($user_input);
Tim Plunkett
committed
}
}
$this->options['expose']['label'] = '';
}
}
/**
* Render our chunk of the exposed filter form when selecting
*
* You can override this if it doesn't do what you expect.
*/
Dries Buytaert
committed
public function buildExposedForm(&$form, FormStateInterface $form_state) {
if (empty($this->options['exposed'])) {
return;
}
// Build the exposed form, when its based on an operator.
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
$operator = $this->options['expose']['operator_id'];
Alex Pott
committed
$this->operatorForm($form, $form_state);
Alex Pott
committed
$this->exposedTranslate($form[$operator], 'operator');
unset($form['operator']);
}
// Build the form and set the value based on the identifier.
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']);
}
Alex Pott
committed
$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']);
}
}
}
Tim Plunkett
committed
/**
* Build the form to let users create the group of exposed filters.
* This form is displayed when users click on button 'Build group'
*/
Dries Buytaert
committed
protected function buildExposedFiltersGroupForm(&$form, FormStateInterface $form_state) {
Tim Plunkett
committed
if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
return;
}
$form['#theme'] = 'views_ui_build_group_filter_form';
// #flatten will move everything from $form['group_info'][$key] to $form[$key]
// prior to rendering. That's why the preRender for it needs to run first,
// so that when the next preRender (the one for fieldsets) runs, it gets
Tim Plunkett
committed
// the flattened data.
Alex Pott
committed
array_unshift($form['#pre_render'], array(get_class($this), 'preRenderFlattenData'));
Tim Plunkett
committed
$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' => t('Filter identifier'),
'#size' => 40,
'#description' => 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' => t('Label'),
'#size' => 40,
);
Damian Lee
committed
$form['group_info']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['description'],
'#title' => t('Description'),
'#size' => 60,
);
Tim Plunkett
committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => 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' => t('Allow multiple selections'),
'#description' => 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' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => 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' => t('Filter identifier'),
'#size' => 40,
'#description' => 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' => t('Label'),
'#size' => 40,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => 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' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
$groups = array('All' => t('- Any -')); // The string '- Any -' will not be rendered see @theme_views_ui_build_group_filter_form
Tim Plunkett
committed
// Provide 3 options to start when we are in a new group.
if (count($this->options['group_info']['group_items']) == 0) {
$this->options['group_info']['group_items'] = array_fill(1, 3, array());
}
// After the general settings, comes a table with all the existent groups.
$default_weight = 0;
foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
Alex Pott
committed
if (!$form_state->isValueEmpty(array('options', 'group_info', 'group_items', $item_id, 'remove'))) {
Tim Plunkett
committed
continue;
}
// Each rows contains three widgets:
// a) The title, where users define how they identify a pair of operator | value
// b) The operator
// c) The value (or values) to use in the filter with the selected operator
// In each row, we have to display the operator form and the value from
// $row acts as a fake form to render each widget in a row.
$row = array();
Angie Byron
committed
$groups[$item_id] = t('Grouping @id', array('@id' => $item_id));
Alex Pott
committed
$this->operatorForm($row, $form_state);
Tim Plunkett
committed
// Force the operator form to be a select box. Some handlers uses
// radios and they occupy a lot of space in a table row.
$row['operator']['#type'] = 'select';
$row['operator']['#title'] = '';
$this->valueForm($row, $form_state);
Tim Plunkett
committed
// Fix the dependencies to update value forms when operators
// changes. This is needed because forms are inside a new form and
// their ids changes. Dependencies are used when operator changes
// from to 'Between', 'Not Between', etc, and two or more widgets
// are displayed.
$without_children = TRUE;
Angie Byron
committed
foreach (Element::children($row['value']) as $children) {
$has_state = FALSE;
$states = array();
foreach ($row['value'][$children]['#states']['visible'] as $key => $state) {
if (isset($state[':input[name="options[operator]"]'])) {
$has_state = TRUE;
$states[$key] = $state[':input[name="options[operator]"]']['value'];
}
}
if ($has_state) {
foreach ($states as $key => $state) {
$row['value'][$children]['#states']['visible'][] = array(
':input[name="options[group_info][group_items][' . $item_id . '][operator]"]' => array('value' => $state),
);
unset($row['value'][$children]['#states']['visible'][$key]);
}