only in patch2: unchanged: --- a/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php +++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php @@ -127,6 +127,20 @@ class EntityAutocomplete extends Textfield { // Store the selection settings in the key/value store and pass a hashed key // in the route parameters. $selection_settings = isset($element['#selection_settings']) ? $element['#selection_settings'] : []; + + $entity = isset($selection_settings['entity']) ? $selection_settings['entity'] : NULL; + if ($entity instanceof EntityInterface) { + // Don't serialise the entity, just pack the ID's. + $entity_info = [ + 'uuid' => $entity->uuid(), + 'entity_type' => $entity->getEntityTypeId(), + ]; + if (!$entity_info['uuid']) { + $entity_info['id'] = $entity->id(); + } + $selection_settings['entity'] = $entity_info; + } + $data = serialize($selection_settings) . $element['#target_type'] . $element['#selection_handler']; $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt()); only in patch2: unchanged: --- a/core/modules/system/src/Controller/EntityAutocompleteController.php +++ b/core/modules/system/src/Controller/EntityAutocompleteController.php @@ -7,6 +7,8 @@ use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\Unicode; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityAutocompleteMatcher; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; use Drupal\Core\Site\Settings; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -100,10 +102,39 @@ class EntityAutocompleteController extends ControllerBase { throw new AccessDeniedHttpException(); } + $entity_info = isset($selection_settings['entity']) ? $selection_settings['entity'] : NULL; + if (is_array($entity_info)) { + $selection_settings['entity'] = $this->loadEntityFromIds($entity_info); + } + $matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, $typed_string); } return new JsonResponse($matches); } + /** + * Loads an entity from storage given entity ID's. + * + * @param array $entity_info + * An array containing keys, where either uuid or ID is required: + * - 'entity_type': Required. An entity type ID. + * - 'uuid': Optional. An entity UUID. + * - 'id': Optional. An entity ID. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The entity loaded from provided ID's, or null if it does not exist. + */ + protected function loadEntityFromIds(array $entity_info) { + $entity_type = $entity_info['entity_type']; + $uuid = !empty($entity_info['uuid']) ? $entity_info['uuid'] : NULL; + if ($uuid) { + return $this->entityManager()->loadEntityByUuid($entity_type, $uuid); + } + else { + return $this->entityManager()->getStorage($entity_type) + ->load($entity_info['id']); + } + } + }