3 namespace Drupal\views\Plugin\views\display;
5 use Drupal\Core\Database\Query\Condition;
8 * The plugin that handles an EntityReference display.
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.
14 * @ingroup views_display_plugins
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
27 class EntityReference extends DisplayPluginBase {
32 protected $usesAJAX = FALSE;
37 protected $usesPager = FALSE;
42 protected $usesAttachments = FALSE;
47 protected function defineOptions() {
48 $options = parent::defineOptions();
50 // Force the style plugin to 'entity_reference_style' and the row plugin to
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;
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;
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']);
77 public function getType() {
78 return 'entity_reference';
84 public function execute() {
85 return $this->view->render($this->display['id']);
89 * Builds the view result as a renderable array.
92 * Renderable array or empty array.
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);
104 public function usesExposed() {
111 public function query() {
112 if (!empty($this->view->live_preview)) {
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);
121 $options = $this->getOption('entity_reference_options');
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;
134 // Multiple search fields are OR'd together.
135 $conditions = new Condition('OR');
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');
150 $this->view->query->addWhere(0, $conditions);
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');
158 $this->view->setItemsPerPage($options['limit']);
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']]);
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]);