EntityAutocomplete.php 16 KB

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