Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views\Plugin\EntityReferenceSelection;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityReferenceSelection\SelectionTrait;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
9 use Drupal\Core\Url;
|
Chris@0
|
10 use Drupal\views\Views;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Plugin implementation of the 'selection' entity_reference.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @EntityReferenceSelection(
|
Chris@0
|
16 * id = "views",
|
Chris@0
|
17 * label = @Translation("Views: Filter by an entity reference view"),
|
Chris@0
|
18 * group = "views",
|
Chris@0
|
19 * weight = 0
|
Chris@0
|
20 * )
|
Chris@0
|
21 */
|
Chris@0
|
22 class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPluginInterface {
|
Chris@0
|
23
|
Chris@0
|
24 use SelectionTrait;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The loaded View object.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Drupal\views\ViewExecutable;
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $view;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public function defaultConfiguration() {
|
Chris@0
|
37 return [
|
Chris@0
|
38 'view' => [
|
Chris@0
|
39 'view_name' => NULL,
|
Chris@0
|
40 'display_name' => NULL,
|
Chris@0
|
41 'arguments' => [],
|
Chris@0
|
42 ],
|
Chris@0
|
43 ] + parent::defaultConfiguration();
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
50 $form = parent::buildConfigurationForm($form, $form_state);
|
Chris@0
|
51
|
Chris@0
|
52 $view_settings = $this->getConfiguration()['view'];
|
Chris@0
|
53 $displays = Views::getApplicableViews('entity_reference_display');
|
Chris@0
|
54 // Filter views that list the entity type we want, and group the separate
|
Chris@0
|
55 // displays by view.
|
Chris@0
|
56 $entity_type = $this->entityManager->getDefinition($this->configuration['target_type']);
|
Chris@0
|
57 $view_storage = $this->entityManager->getStorage('view');
|
Chris@0
|
58
|
Chris@0
|
59 $options = [];
|
Chris@0
|
60 foreach ($displays as $data) {
|
Chris@0
|
61 list($view_id, $display_id) = $data;
|
Chris@0
|
62 $view = $view_storage->load($view_id);
|
Chris@0
|
63 if (in_array($view->get('base_table'), [$entity_type->getBaseTable(), $entity_type->getDataTable()])) {
|
Chris@0
|
64 $display = $view->get('display');
|
Chris@0
|
65 $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 // The value of the 'view_and_display' select below will need to be split
|
Chris@0
|
70 // into 'view_name' and 'view_display' in the final submitted values, so
|
Chris@0
|
71 // we massage the data at validate time on the wrapping element (not
|
Chris@0
|
72 // ideal).
|
Chris@0
|
73 $form['view']['#element_validate'] = [[get_called_class(), 'settingsFormValidate']];
|
Chris@0
|
74
|
Chris@0
|
75 if ($options) {
|
Chris@0
|
76 $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
|
Chris@0
|
77 $form['view']['view_and_display'] = [
|
Chris@0
|
78 '#type' => 'select',
|
Chris@0
|
79 '#title' => $this->t('View used to select the entities'),
|
Chris@0
|
80 '#required' => TRUE,
|
Chris@0
|
81 '#options' => $options,
|
Chris@0
|
82 '#default_value' => $default,
|
Chris@0
|
83 '#description' => '<p>' . $this->t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
|
Chris@0
|
84 ];
|
Chris@0
|
85
|
Chris@0
|
86 $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
|
Chris@0
|
87 $form['view']['arguments'] = [
|
Chris@0
|
88 '#type' => 'textfield',
|
Chris@0
|
89 '#title' => $this->t('View arguments'),
|
Chris@0
|
90 '#default_value' => $default,
|
Chris@0
|
91 '#required' => FALSE,
|
Chris@0
|
92 '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.'),
|
Chris@0
|
93 ];
|
Chris@0
|
94 }
|
Chris@0
|
95 else {
|
Chris@0
|
96 if ($this->currentUser->hasPermission('administer views') && $this->moduleHandler->moduleExists('views_ui')) {
|
Chris@0
|
97 $form['view']['no_view_help'] = [
|
Chris@0
|
98 '#markup' => '<p>' . $this->t('No eligible views were found. <a href=":create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href=":existing">existing view</a>.', [
|
Chris@0
|
99 ':create' => Url::fromRoute('views_ui.add')->toString(),
|
Chris@0
|
100 ':existing' => Url::fromRoute('entity.view.collection')->toString(),
|
Chris@0
|
101 ]) . '</p>',
|
Chris@0
|
102 ];
|
Chris@0
|
103 }
|
Chris@0
|
104 else {
|
Chris@0
|
105 $form['view']['no_view_help']['#markup'] = '<p>' . $this->t('No eligible views were found.') . '</p>';
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108 return $form;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Initializes a view.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param string|null $match
|
Chris@0
|
115 * (Optional) Text to match the label against. Defaults to NULL.
|
Chris@0
|
116 * @param string $match_operator
|
Chris@0
|
117 * (Optional) The operation the matching should be done with. Defaults
|
Chris@0
|
118 * to "CONTAINS".
|
Chris@0
|
119 * @param int $limit
|
Chris@0
|
120 * Limit the query to a given number of items. Defaults to 0, which
|
Chris@0
|
121 * indicates no limiting.
|
Chris@0
|
122 * @param array|null $ids
|
Chris@0
|
123 * Array of entity IDs. Defaults to NULL.
|
Chris@0
|
124 *
|
Chris@0
|
125 * @return bool
|
Chris@0
|
126 * Return TRUE if the view was initialized, FALSE otherwise.
|
Chris@0
|
127 */
|
Chris@0
|
128 protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
|
Chris@0
|
129 $view_name = $this->getConfiguration()['view']['view_name'];
|
Chris@0
|
130 $display_name = $this->getConfiguration()['view']['display_name'];
|
Chris@0
|
131
|
Chris@0
|
132 // Check that the view is valid and the display still exists.
|
Chris@0
|
133 $this->view = Views::getView($view_name);
|
Chris@0
|
134 if (!$this->view || !$this->view->access($display_name)) {
|
Chris@0
|
135 drupal_set_message(t('The reference view %view_name cannot be found.', ['%view_name' => $view_name]), 'warning');
|
Chris@0
|
136 return FALSE;
|
Chris@0
|
137 }
|
Chris@0
|
138 $this->view->setDisplay($display_name);
|
Chris@0
|
139
|
Chris@0
|
140 // Pass options to the display handler to make them available later.
|
Chris@0
|
141 $entity_reference_options = [
|
Chris@0
|
142 'match' => $match,
|
Chris@0
|
143 'match_operator' => $match_operator,
|
Chris@0
|
144 'limit' => $limit,
|
Chris@0
|
145 'ids' => $ids,
|
Chris@0
|
146 ];
|
Chris@0
|
147 $this->view->displayHandlers->get($display_name)->setOption('entity_reference_options', $entity_reference_options);
|
Chris@0
|
148 return TRUE;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * {@inheritdoc}
|
Chris@0
|
153 */
|
Chris@0
|
154 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
|
Chris@0
|
155 $display_name = $this->getConfiguration()['view']['display_name'];
|
Chris@0
|
156 $arguments = $this->getConfiguration()['view']['arguments'];
|
Chris@0
|
157 $result = [];
|
Chris@0
|
158 if ($this->initializeView($match, $match_operator, $limit)) {
|
Chris@0
|
159 // Get the results.
|
Chris@0
|
160 $result = $this->view->executeDisplay($display_name, $arguments);
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 $return = [];
|
Chris@0
|
164 if ($result) {
|
Chris@0
|
165 foreach ($this->view->result as $row) {
|
Chris@0
|
166 $entity = $row->_entity;
|
Chris@0
|
167 $return[$entity->bundle()][$entity->id()] = $entity->label();
|
Chris@0
|
168 }
|
Chris@0
|
169 }
|
Chris@0
|
170 return $return;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * {@inheritdoc}
|
Chris@0
|
175 */
|
Chris@0
|
176 public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
|
Chris@0
|
177 $this->getReferenceableEntities($match, $match_operator);
|
Chris@0
|
178 return $this->view->pager->getTotalItems();
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * {@inheritdoc}
|
Chris@0
|
183 */
|
Chris@0
|
184 public function validateReferenceableEntities(array $ids) {
|
Chris@0
|
185 $display_name = $this->getConfiguration()['view']['display_name'];
|
Chris@0
|
186 $arguments = $this->getConfiguration()['view']['arguments'];
|
Chris@0
|
187 $result = [];
|
Chris@0
|
188 if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
|
Chris@0
|
189 // Get the results.
|
Chris@0
|
190 $entities = $this->view->executeDisplay($display_name, $arguments);
|
Chris@0
|
191 $result = array_keys($entities);
|
Chris@0
|
192 }
|
Chris@0
|
193 return $result;
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Element validate; Check View is valid.
|
Chris@0
|
198 */
|
Chris@0
|
199 public static function settingsFormValidate($element, FormStateInterface $form_state, $form) {
|
Chris@0
|
200 // Split view name and display name from the 'view_and_display' value.
|
Chris@0
|
201 if (!empty($element['view_and_display']['#value'])) {
|
Chris@0
|
202 list($view, $display) = explode(':', $element['view_and_display']['#value']);
|
Chris@0
|
203 }
|
Chris@0
|
204 else {
|
Chris@0
|
205 $form_state->setError($element, t('The views entity selection mode requires a view.'));
|
Chris@0
|
206 return;
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 // Explode the 'arguments' string into an actual array. Beware, explode()
|
Chris@0
|
210 // turns an empty string into an array with one empty string. We'll need an
|
Chris@0
|
211 // empty array instead.
|
Chris@0
|
212 $arguments_string = trim($element['arguments']['#value']);
|
Chris@0
|
213 if ($arguments_string === '') {
|
Chris@0
|
214 $arguments = [];
|
Chris@0
|
215 }
|
Chris@0
|
216 else {
|
Chris@0
|
217 // array_map() is called to trim whitespaces from the arguments.
|
Chris@0
|
218 $arguments = array_map('trim', explode(',', $arguments_string));
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 $value = ['view_name' => $view, 'display_name' => $display, 'arguments' => $arguments];
|
Chris@0
|
222 $form_state->setValueForElement($element, $value);
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 }
|