3 namespace Drupal\views\Plugin\views\argument;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ManyToOneHelper;
11 * An argument handler for use in fields that have a many to one relationship
12 * with the table(s) to the left. This adds a bunch of options that are
13 * reasonably common with this type of relationship.
15 * - numeric: If true, the field will be considered numeric. Probably should
16 * always be set TRUE as views_handler_argument_string has many to one
18 * - zero is null: If true, a 0 will be handled as empty, so for example
19 * a default argument can be provided or a summary can be shown.
21 * @ingroup views_argument_handlers
23 * @ViewsArgument("many_to_one")
25 class ManyToOne extends ArgumentPluginBase {
30 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
31 parent::init($view, $display, $options);
33 $this->helper = new ManyToOneHelper($this);
35 // Ensure defaults for these, during summaries and stuff:
36 $this->operator = 'or';
40 protected function defineOptions() {
41 $options = parent::defineOptions();
43 if (!empty($this->definition['numeric'])) {
44 $options['break_phrase'] = ['default' => FALSE];
47 $options['add_table'] = ['default' => FALSE];
48 $options['require_value'] = ['default' => FALSE];
50 if (isset($this->helper)) {
51 $this->helper->defineOptions($options);
54 $helper = new ManyToOneHelper($this);
55 $helper->defineOptions($options);
61 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
62 parent::buildOptionsForm($form, $form_state);
64 // allow + for or, , for and
65 $form['break_phrase'] = [
66 '#type' => 'checkbox',
67 '#title' => $this->t('Allow multiple values'),
68 '#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).'),
69 '#default_value' => !empty($this->options['break_phrase']),
70 '#group' => 'options][more',
73 $form['add_table'] = [
74 '#type' => 'checkbox',
75 '#title' => $this->t('Allow multiple filter values to work together'),
76 '#description' => $this->t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
77 '#default_value' => !empty($this->options['add_table']),
78 '#group' => 'options][more',
81 $form['require_value'] = [
82 '#type' => 'checkbox',
83 '#title' => $this->t('Do not display items with no value in summary'),
84 '#default_value' => !empty($this->options['require_value']),
85 '#group' => 'options][more',
88 $this->helper->buildOptionsForm($form, $form_state);
92 * Override ensureMyTable so we can control how this joins in.
93 * The operator actually has influence over joining.
95 public function ensureMyTable() {
96 $this->helper->ensureMyTable();
99 public function query($group_by = FALSE) {
101 if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
102 if (empty($this->argument)) {
107 if (!isset($this->argument)) {
112 parent::ensureMyTable();
113 $this->query->addWhere(0, "$this->tableAlias.$this->realField", NULL, 'IS NULL');
117 if (!empty($this->options['break_phrase'])) {
118 $force_int = !empty($this->definition['numeric']);
119 $this->unpackArgumentValue($force_int);
122 $this->value = [$this->argument];
123 $this->operator = 'or';
126 $this->helper->addFilter();
129 public function title() {
130 if (!$this->argument) {
131 return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
134 if (!empty($this->options['break_phrase'])) {
135 $force_int = !empty($this->definition['numeric']);
136 $this->unpackArgumentValue($force_int);
139 $this->value = [$this->argument];
140 $this->operator = 'or';
143 // @todo -- both of these should check definition for alternate keywords.
145 if (empty($this->value)) {
146 return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
149 if ($this->value === [-1]) {
150 return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this->t('Invalid input');
153 return implode($this->operator == 'or' ? ' + ' : ', ', $this->titleQuery());
156 protected function summaryQuery() {
157 $field = $this->table . '.' . $this->field;
158 $join = $this->getJoin();
160 if (!empty($this->options['require_value'])) {
161 $join->type = 'INNER';
164 if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
165 $this->tableAlias = $this->query->ensureTable($this->table, $this->relationship, $join);
168 $this->tableAlias = $this->helper->summaryJoin();
172 $this->base_alias = $this->query->addField($this->tableAlias, $this->realField);
174 $this->summaryNameField();
176 return $this->summaryBasics();
179 public function summaryArgument($data) {
180 $value = $data->{$this->base_alias};
189 * Override for specific title lookups.
191 public function titleQuery() {