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