3 namespace Drupal\views\Plugin\views\display;
6 * The plugin that handles an EntityReference display.
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.
12 * @ingroup views_display_plugins
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
25 class EntityReference extends DisplayPluginBase {
30 protected $usesAJAX = FALSE;
35 protected $usesPager = FALSE;
40 protected $usesAttachments = FALSE;
45 protected function defineOptions() {
46 $options = parent::defineOptions();
48 // Force the style plugin to 'entity_reference_style' and the row plugin to
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;
55 // Make sure the query is not cached.
56 $options['defaults']['default']['cache'] = FALSE;
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;
66 * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummary().
68 * Disable 'cache' and 'title' so it won't be changed.
70 public function optionsSummary(&$categories, &$options) {
71 parent::optionsSummary($categories, $options);
72 unset($options['query']);
73 unset($options['title']);
79 public function getType() {
80 return 'entity_reference';
86 public function execute() {
87 return $this->view->render($this->display['id']);
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);
103 public function usesExposed() {
110 public function query() {
111 if (!empty($this->view->live_preview)) {
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);
120 $options = $this->getOption('entity_reference_options');
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;
133 // Multiple search fields are OR'd together.
134 $conditions = db_or();
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');
149 $this->view->query->addWhere(0, $conditions);
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');
157 $this->view->setItemsPerPage($options['limit']);
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']]);
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]);