Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / src / Plugin / views / display / EntityReference.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\display;
4
5 use Drupal\Core\Database\Query\Condition;
6
7 /**
8  * The plugin that handles an EntityReference display.
9  *
10  * "entity_reference_display" is a custom property, used with
11  * \Drupal\views\Views::getApplicableViews() to retrieve all views with a
12  * 'Entity Reference' display.
13  *
14  * @ingroup views_display_plugins
15  *
16  * @ViewsDisplay(
17  *   id = "entity_reference",
18  *   title = @Translation("Entity Reference"),
19  *   admin = @Translation("Entity Reference Source"),
20  *   help = @Translation("Selects referenceable entities for an entity reference field."),
21  *   theme = "views_view",
22  *   register_theme = FALSE,
23  *   uses_menu_links = FALSE,
24  *   entity_reference_display = TRUE
25  * )
26  */
27 class EntityReference extends DisplayPluginBase {
28
29   /**
30    * {@inheritdoc}
31    */
32   protected $usesAJAX = FALSE;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected $usesPager = FALSE;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected $usesAttachments = FALSE;
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function defineOptions() {
48     $options = parent::defineOptions();
49
50     // Force the style plugin to 'entity_reference_style' and the row plugin to
51     // 'fields'.
52     $options['style']['contains']['type'] = ['default' => 'entity_reference'];
53     $options['defaults']['default']['style'] = FALSE;
54     $options['row']['contains']['type'] = ['default' => 'entity_reference'];
55     $options['defaults']['default']['row'] = FALSE;
56
57     // Set the display title to an empty string (not used in this display type).
58     $options['title']['default'] = '';
59     $options['defaults']['default']['title'] = FALSE;
60
61     return $options;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function optionsSummary(&$categories, &$options) {
68     parent::optionsSummary($categories, $options);
69     // Disable 'title' so it won't be changed from the default set in
70     // \Drupal\views\Plugin\views\display\EntityReference::defineOptions.
71     unset($options['title']);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getType() {
78     return 'entity_reference';
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function execute() {
85     return $this->view->render($this->display['id']);
86   }
87
88   /**
89    * Builds the view result as a renderable array.
90    *
91    * @return array
92    *   Renderable array or empty array.
93    */
94   public function render() {
95     if (!empty($this->view->result) && $this->view->style_plugin->evenEmpty()) {
96       return $this->view->style_plugin->render($this->view->result);
97     }
98     return [];
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function usesExposed() {
105     return FALSE;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function query() {
112     if (!empty($this->view->live_preview)) {
113       return;
114     }
115
116     // Make sure the id field is included in the results.
117     $id_field = $this->view->storage->get('base_field');
118     $id_table = $this->view->storage->get('base_table');
119     $this->id_field_alias = $this->view->query->addField($id_table, $id_field);
120
121     $options = $this->getOption('entity_reference_options');
122
123     // Restrict the autocomplete options based on what's been typed already.
124     if (isset($options['match'])) {
125       $style_options = $this->getOption('style');
126       $value = db_like($options['match']);
127       if ($options['match_operator'] !== '=') {
128         $value = $value . '%';
129         if ($options['match_operator'] != 'STARTS_WITH') {
130           $value = '%' . $value;
131         }
132       }
133
134       // Multiple search fields are OR'd together.
135       $conditions = new Condition('OR');
136
137       // Build the condition using the selected search fields.
138       foreach ($style_options['options']['search_fields'] as $field_id) {
139         if (!empty($field_id)) {
140           // Get the table and field names for the checked field.
141           $field_handler = $this->view->field[$field_id];
142           $table_alias = $this->view->query->ensureTable($field_handler->table, $field_handler->relationship);
143           $field_alias = $this->view->query->addField($table_alias, $field_handler->realField);
144           $field = $this->view->query->fields[$field_alias];
145           // Add an OR condition for the field.
146           $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
147         }
148       }
149
150       $this->view->query->addWhere(0, $conditions);
151     }
152
153     // Add an IN condition for validation.
154     if (!empty($options['ids'])) {
155       $this->view->query->addWhere(0, $id_table . '.' . $id_field, $options['ids'], 'IN');
156     }
157
158     $this->view->setItemsPerPage($options['limit']);
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function validate() {
165     $errors = parent::validate();
166     // Verify that search fields are set up.
167     $style = $this->getOption('style');
168     if (!isset($style['options']['search_fields'])) {
169       $errors[] = $this->t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', ['@display' => $this->display['display_title']]);
170     }
171     else {
172       // Verify that the search fields used actually exist.
173       $fields = array_keys($this->handlers['field']);
174       foreach ($style['options']['search_fields'] as $field_alias => $enabled) {
175         if ($enabled && !in_array($field_alias, $fields)) {
176           $errors[] = $this->t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', ['@display' => $this->display['display_title'], '%field' => $field_alias]);
177         }
178       }
179     }
180     return $errors;
181   }
182
183 }