Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Element / EntityAutocomplete.php
1 <?php
2
3 namespace Drupal\Core\Entity\Element;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\Tags;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
9 use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Render\Element\Textfield;
12 use Drupal\Core\Site\Settings;
13
14 /**
15  * Provides an entity autocomplete form element.
16  *
17  * The #default_value accepted by this element is either an entity object or an
18  * array of entity objects.
19  *
20  * @FormElement("entity_autocomplete")
21  */
22 class EntityAutocomplete extends Textfield {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getInfo() {
28     $info = parent::getInfo();
29     $class = get_class($this);
30
31     // Apply default form element properties.
32     $info['#target_type'] = NULL;
33     $info['#selection_handler'] = 'default';
34     $info['#selection_settings'] = [];
35     $info['#tags'] = FALSE;
36     $info['#autocreate'] = NULL;
37     // This should only be set to FALSE if proper validation by the selection
38     // handler is performed at another level on the extracted form values.
39     $info['#validate_reference'] = TRUE;
40     // IMPORTANT! This should only be set to FALSE if the #default_value
41     // property is processed at another level (e.g. by a Field API widget) and
42     // its value is properly checked for access.
43     $info['#process_default_value'] = TRUE;
44
45     $info['#element_validate'] = [[$class, 'validateEntityAutocomplete']];
46     array_unshift($info['#process'], [$class, 'processEntityAutocomplete']);
47
48     return $info;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
55     // Process the #default_value property.
56     if ($input === FALSE && isset($element['#default_value']) && $element['#process_default_value']) {
57       if (is_array($element['#default_value']) && $element['#tags'] !== TRUE) {
58         throw new \InvalidArgumentException('The #default_value property is an array but the form element does not allow multiple values.');
59       }
60       elseif (!empty($element['#default_value']) && !is_array($element['#default_value'])) {
61         // Convert the default value into an array for easier processing in
62         // static::getEntityLabels().
63         $element['#default_value'] = [$element['#default_value']];
64       }
65
66       if ($element['#default_value']) {
67         if (!(reset($element['#default_value']) instanceof EntityInterface)) {
68           throw new \InvalidArgumentException('The #default_value property has to be an entity object or an array of entity objects.');
69         }
70
71         // Extract the labels from the passed-in entity objects, taking access
72         // checks into account.
73         return static::getEntityLabels($element['#default_value']);
74       }
75     }
76
77     // Potentially the #value is set directly, so it contains the 'target_id'
78     // array structure instead of a string.
79     if ($input !== FALSE && is_array($input)) {
80       $entity_ids = array_map(function (array $item) {
81         return $item['target_id'];
82       }, $input);
83
84       $entities = \Drupal::entityTypeManager()->getStorage($element['#target_type'])->loadMultiple($entity_ids);
85
86       return static::getEntityLabels($entities);
87     }
88   }
89
90   /**
91    * Adds entity autocomplete functionality to a form element.
92    *
93    * @param array $element
94    *   The form element to process. Properties used:
95    *   - #target_type: The ID of the target entity type.
96    *   - #selection_handler: The plugin ID of the entity reference selection
97    *     handler.
98    *   - #selection_settings: An array of settings that will be passed to the
99    *     selection handler.
100    * @param \Drupal\Core\Form\FormStateInterface $form_state
101    *   The current state of the form.
102    * @param array $complete_form
103    *   The complete form structure.
104    *
105    * @return array
106    *   The form element.
107    *
108    * @throws \InvalidArgumentException
109    *   Exception thrown when the #target_type or #autocreate['bundle'] are
110    *   missing.
111    */
112   public static function processEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
113     // Nothing to do if there is no target entity type.
114     if (empty($element['#target_type'])) {
115       throw new \InvalidArgumentException('Missing required #target_type parameter.');
116     }
117
118     // Provide default values and sanity checks for the #autocreate parameter.
119     if ($element['#autocreate']) {
120       if (!isset($element['#autocreate']['bundle'])) {
121         throw new \InvalidArgumentException("Missing required #autocreate['bundle'] parameter.");
122       }
123       // Default the autocreate user ID to the current user.
124       $element['#autocreate']['uid'] = isset($element['#autocreate']['uid']) ? $element['#autocreate']['uid'] : \Drupal::currentUser()->id();
125     }
126
127     // Store the selection settings in the key/value store and pass a hashed key
128     // in the route parameters.
129     $selection_settings = isset($element['#selection_settings']) ? $element['#selection_settings'] : [];
130     $data = serialize($selection_settings) . $element['#target_type'] . $element['#selection_handler'];
131     $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
132
133     $key_value_storage = \Drupal::keyValue('entity_autocomplete');
134     if (!$key_value_storage->has($selection_settings_key)) {
135       $key_value_storage->set($selection_settings_key, $selection_settings);
136     }
137
138     $element['#autocomplete_route_name'] = 'system.entity_autocomplete';
139     $element['#autocomplete_route_parameters'] = [
140       'target_type' => $element['#target_type'],
141       'selection_handler' => $element['#selection_handler'],
142       'selection_settings_key' => $selection_settings_key,
143     ];
144
145     return $element;
146   }
147
148   /**
149    * Form element validation handler for entity_autocomplete elements.
150    */
151   public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
152     $value = NULL;
153
154     if (!empty($element['#value'])) {
155       $options = $element['#selection_settings'] + [
156         'target_type' => $element['#target_type'],
157         'handler' => $element['#selection_handler'],
158       ];
159       /** @var /Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */
160       $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($options);
161       $autocreate = (bool) $element['#autocreate'] && $handler instanceof SelectionWithAutocreateInterface;
162
163       // GET forms might pass the validated data around on the next request, in
164       // which case it will already be in the expected format.
165       if (is_array($element['#value'])) {
166         $value = $element['#value'];
167       }
168       else {
169         $input_values = $element['#tags'] ? Tags::explode($element['#value']) : [$element['#value']];
170
171         foreach ($input_values as $input) {
172           $match = static::extractEntityIdFromAutocompleteInput($input);
173           if ($match === NULL) {
174             // Try to get a match from the input string when the user didn't use
175             // the autocomplete but filled in a value manually.
176             $match = static::matchEntityByTitle($handler, $input, $element, $form_state, !$autocreate);
177           }
178
179           if ($match !== NULL) {
180             $value[] = [
181               'target_id' => $match,
182             ];
183           }
184           elseif ($autocreate) {
185             /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface $handler */
186             // Auto-create item. See an example of how this is handled in
187             // \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::presave().
188             $value[] = [
189               'entity' => $handler->createNewEntity($element['#target_type'], $element['#autocreate']['bundle'], $input, $element['#autocreate']['uid']),
190             ];
191           }
192         }
193       }
194
195       // Check that the referenced entities are valid, if needed.
196       if ($element['#validate_reference'] && !empty($value)) {
197         // Validate existing entities.
198         $ids = array_reduce($value, function ($return, $item) {
199           if (isset($item['target_id'])) {
200             $return[] = $item['target_id'];
201           }
202           return $return;
203         });
204
205         if ($ids) {
206           $valid_ids = $handler->validateReferenceableEntities($ids);
207           if ($invalid_ids = array_diff($ids, $valid_ids)) {
208             foreach ($invalid_ids as $invalid_id) {
209               $form_state->setError($element, t('The referenced entity (%type: %id) does not exist.', ['%type' => $element['#target_type'], '%id' => $invalid_id]));
210             }
211           }
212         }
213
214         // Validate newly created entities.
215         $new_entities = array_reduce($value, function ($return, $item) {
216           if (isset($item['entity'])) {
217             $return[] = $item['entity'];
218           }
219           return $return;
220         });
221
222         if ($new_entities) {
223           if ($autocreate) {
224             $valid_new_entities = $handler->validateReferenceableNewEntities($new_entities);
225             $invalid_new_entities = array_diff_key($new_entities, $valid_new_entities);
226           }
227           else {
228             // If the selection handler does not support referencing newly
229             // created entities, all of them should be invalidated.
230             $invalid_new_entities = $new_entities;
231           }
232
233           foreach ($invalid_new_entities as $entity) {
234             /** @var \Drupal\Core\Entity\EntityInterface $entity */
235             $form_state->setError($element, t('This entity (%type: %label) cannot be referenced.', ['%type' => $element['#target_type'], '%label' => $entity->label()]));
236           }
237         }
238       }
239
240       // Use only the last value if the form element does not support multiple
241       // matches (tags).
242       if (!$element['#tags'] && !empty($value)) {
243         $last_value = $value[count($value) - 1];
244         $value = isset($last_value['target_id']) ? $last_value['target_id'] : $last_value;
245       }
246     }
247
248     $form_state->setValueForElement($element, $value);
249   }
250
251   /**
252    * Finds an entity from an autocomplete input without an explicit ID.
253    *
254    * The method will return an entity ID if one single entity unambiguously
255    * matches the incoming input, and assign form errors otherwise.
256    *
257    * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler
258    *   Entity reference selection plugin.
259    * @param string $input
260    *   Single string from autocomplete element.
261    * @param array $element
262    *   The form element to set a form error.
263    * @param \Drupal\Core\Form\FormStateInterface $form_state
264    *   The current form state.
265    * @param bool $strict
266    *   Whether to trigger a form error if an element from $input (eg. an entity)
267    *   is not found.
268    *
269    * @return int|null
270    *   Value of a matching entity ID, or NULL if none.
271    */
272   protected static function matchEntityByTitle(SelectionInterface $handler, $input, array &$element, FormStateInterface $form_state, $strict) {
273     $entities_by_bundle = $handler->getReferenceableEntities($input, '=', 6);
274     $entities = array_reduce($entities_by_bundle, function ($flattened, $bundle_entities) {
275       return $flattened + $bundle_entities;
276     }, []);
277     $params = [
278       '%value' => $input,
279       '@value' => $input,
280     ];
281     if (empty($entities)) {
282       if ($strict) {
283         // Error if there are no entities available for a required field.
284         $form_state->setError($element, t('There are no entities matching "%value".', $params));
285       }
286     }
287     elseif (count($entities) > 5) {
288       $params['@id'] = key($entities);
289       // Error if there are more than 5 matching entities.
290       $form_state->setError($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
291     }
292     elseif (count($entities) > 1) {
293       // More helpful error if there are only a few matching entities.
294       $multiples = [];
295       foreach ($entities as $id => $name) {
296         $multiples[] = $name . ' (' . $id . ')';
297       }
298       $params['@id'] = $id;
299       $form_state->setError($element, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', ['%multiple' => implode('", "', $multiples)] + $params));
300     }
301     else {
302       // Take the one and only matching entity.
303       return key($entities);
304     }
305   }
306
307   /**
308    * Converts an array of entity objects into a string of entity labels.
309    *
310    * This method is also responsible for checking the 'view label' access on the
311    * passed-in entities.
312    *
313    * @param \Drupal\Core\Entity\EntityInterface[] $entities
314    *   An array of entity objects.
315    *
316    * @return string
317    *   A string of entity labels separated by commas.
318    */
319   public static function getEntityLabels(array $entities) {
320     /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
321     $entity_repository = \Drupal::service('entity.repository');
322
323     $entity_labels = [];
324     foreach ($entities as $entity) {
325       // Set the entity in the correct language for display.
326       $entity = $entity_repository->getTranslationFromContext($entity);
327
328       // Use the special view label, since some entities allow the label to be
329       // viewed, even if the entity is not allowed to be viewed.
330       $label = ($entity->access('view label')) ? $entity->label() : t('- Restricted access -');
331
332       // Take into account "autocreated" entities.
333       if (!$entity->isNew()) {
334         $label .= ' (' . $entity->id() . ')';
335       }
336
337       // Labels containing commas or quotes must be wrapped in quotes.
338       $entity_labels[] = Tags::encode($label);
339     }
340
341     return implode(', ', $entity_labels);
342   }
343
344   /**
345    * Extracts the entity ID from the autocompletion result.
346    *
347    * @param string $input
348    *   The input coming from the autocompletion result.
349    *
350    * @return mixed|null
351    *   An entity ID or NULL if the input does not contain one.
352    */
353   public static function extractEntityIdFromAutocompleteInput($input) {
354     $match = NULL;
355
356     // Take "label (entity id)', match the ID from inside the parentheses.
357     // @todo Add support for entities containing parentheses in their ID.
358     // @see https://www.drupal.org/node/2520416
359     if (preg_match("/.+\s\(([^\)]+)\)/", $input, $matches)) {
360       $match = $matches[1];
361     }
362
363     return $match;
364   }
365
366 }