View source
<?php
namespace Drupal\search_api\Plugin\views\argument;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
class SearchApiStandard extends ArgumentPluginBase {
public $query;
public $operator;
public function defaultActions($which = NULL) {
$defaults = parent::defaultActions();
unset($defaults['summary']);
if ($which) {
return $defaults[$which] ?? NULL;
}
return $defaults;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['break_phrase'] = [
'default' => FALSE,
];
$options['not'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if (empty($this->definition['disable_break_phrase'])) {
$form['break_phrase'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow multiple values'),
'#description' => $this
->t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
'#default_value' => !empty($this->options['break_phrase']),
'#group' => 'options][more',
];
}
$form['not'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude'),
'#description' => $this
->t('If selected, the values entered for the filter will be excluded rather than limiting the view to those values.'),
'#default_value' => !empty($this->options['not']),
'#group' => 'options][more',
];
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$option_values =& $form_state
->getValue('options');
if (empty($option_values)) {
return;
}
if (!empty($option_values['default_argument_type'])) {
$default_id = $option_values['default_argument_type'];
$plugin = $this
->getPlugin('argument_default', $default_id);
if ($plugin) {
$plugin
->validateOptionsForm($form['argument_default'][$default_id], $form_state, $option_values['argument_default'][$default_id]);
}
}
if (!empty($option_values['validate']['type'])) {
$sanitized_id = $option_values['validate']['type'];
$validate_id = static::decodeValidatorId($sanitized_id);
$plugin = $this
->getPlugin('argument_validator', $validate_id);
if ($plugin) {
$plugin
->validateOptionsForm($form['validate']['options'][$sanitized_id], $form_state, $option_values['validate']['options'][$sanitized_id]);
}
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$option_values =& $form_state
->getValue('options');
if (empty($option_values)) {
return;
}
if (!empty($option_values['default_argument_type'])) {
$default_id = $option_values['default_argument_type'];
$plugin = $this
->getPlugin('argument_default', $default_id);
if ($plugin) {
$options =& $option_values['argument_default'][$default_id];
$plugin
->submitOptionsForm($form['argument_default'][$default_id], $form_state, $options);
$option_values['default_argument_options'] = $options;
}
}
if (empty($option_values['specify_validation'])) {
$option_values['validate']['type'] = 'none';
$option_values['validate']['options'] = [
'none' => [],
];
$option_values['validate']['fail'] = 'not found';
}
if (!empty($option_values['validate']['type'])) {
$sanitized_id = $option_values['validate']['type'];
$option_values['validate']['type'] = $validate_id = static::decodeValidatorId($sanitized_id);
$plugin = $this
->getPlugin('argument_validator', $validate_id);
if ($plugin) {
$options =& $option_values['validate']['options'][$sanitized_id];
$plugin
->submitOptionsForm($form['validate']['options'][$sanitized_id], $form_state, $options);
$option_values['validate_options'] = $options;
}
}
if (empty($option_values['title_enable'])) {
$option_values['title'] = '';
}
}
public function query($group_by = FALSE) {
$this
->fillValue();
if (count($this->value) > 1) {
if ($this->operator === 'or') {
$operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
$this->query
->addCondition($this->realField, $this->value, $operator);
}
else {
foreach ($this->value as $value) {
$operator = empty($this->options['not']) ? '=' : '<>';
$this->query
->addCondition($this->realField, $value, $operator);
}
}
}
elseif ($this->value) {
$operator = empty($this->options['not']) ? '=' : '<>';
$this->query
->addCondition($this->realField, reset($this->value), $operator);
}
}
protected function fillValue() {
if (isset($this->value)) {
return;
}
$filter = '';
if (!empty($this->definition['filter'])) {
$filter = $this->definition['filter'];
}
if (!empty($this->options['break_phrase']) && empty($this->definition['disable_break_phrase'])) {
$force_int = FALSE;
if ($filter == 'intval') {
$force_int = TRUE;
$filter = '';
}
$this
->unpackArgumentValue($force_int);
}
else {
$this->value = [
$this->argument,
];
$this->operator = 'and';
}
if (is_callable($filter)) {
$this->value = array_map($filter, $this->value);
}
}
}