View source
<?php
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ManyToOneHelper;
class ManyToOne extends InOperator {
var $helper = NULL;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->helper = new ManyToOneHelper($this);
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator']['default'] = 'or';
$options['value']['default'] = array();
if (isset($this->helper)) {
$this->helper
->defineOptions($options);
}
else {
$helper = new ManyToOneHelper($this);
$helper
->defineOptions($options);
}
return $options;
}
function operators() {
$operators = array(
'or' => array(
'title' => $this
->t('Is one of'),
'short' => $this
->t('or'),
'short_single' => $this
->t('='),
'method' => 'opHelper',
'values' => 1,
'ensure_my_table' => 'helper',
),
'and' => array(
'title' => $this
->t('Is all of'),
'short' => $this
->t('and'),
'short_single' => $this
->t('='),
'method' => 'opHelper',
'values' => 1,
'ensure_my_table' => 'helper',
),
'not' => array(
'title' => $this
->t('Is none of'),
'short' => $this
->t('not'),
'short_single' => $this
->t('<>'),
'method' => 'opHelper',
'values' => 1,
'ensure_my_table' => 'helper',
),
);
if (!empty($this->definition['allow empty'])) {
$operators += array(
'empty' => array(
'title' => $this
->t('Is empty (NULL)'),
'method' => 'opEmpty',
'short' => $this
->t('empty'),
'values' => 0,
),
'not empty' => array(
'title' => $this
->t('Is not empty (NOT NULL)'),
'method' => 'opEmpty',
'short' => $this
->t('not empty'),
'values' => 0,
),
);
}
return $operators;
}
protected $valueFormType = 'select';
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if (!$form_state
->get('exposed')) {
$this->helper
->buildOptionsForm($form, $form_state);
}
}
public function ensureMyTable() {
$info = $this
->operators();
if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
return $this->helper
->ensureMyTable();
}
return parent::ensureMyTable();
}
protected function opHelper() {
if (empty($this->value)) {
return;
}
$this->helper
->addFilter();
}
}