annotate core/modules/system/src/Controller/EntityAutocompleteController.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Controller;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Crypt;
Chris@0 6 use Drupal\Component\Utility\Tags;
Chris@0 7 use Drupal\Component\Utility\Unicode;
Chris@0 8 use Drupal\Core\Controller\ControllerBase;
Chris@0 9 use Drupal\Core\Entity\EntityAutocompleteMatcher;
Chris@0 10 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
Chris@0 11 use Drupal\Core\Site\Settings;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13 use Symfony\Component\HttpFoundation\JsonResponse;
Chris@0 14 use Symfony\Component\HttpFoundation\Request;
Chris@0 15 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Defines a route controller for entity autocomplete form elements.
Chris@0 19 */
Chris@0 20 class EntityAutocompleteController extends ControllerBase {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The autocomplete matcher for entity references.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Entity\EntityAutocompleteMatcher
Chris@0 26 */
Chris@0 27 protected $matcher;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The key value store.
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
Chris@0 33 */
Chris@0 34 protected $keyValue;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Constructs a EntityAutocompleteController object.
Chris@0 38 *
Chris@0 39 * @param \Drupal\Core\Entity\EntityAutocompleteMatcher $matcher
Chris@0 40 * The autocomplete matcher for entity references.
Chris@0 41 * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value
Chris@0 42 * The key value factory.
Chris@0 43 */
Chris@0 44 public function __construct(EntityAutocompleteMatcher $matcher, KeyValueStoreInterface $key_value) {
Chris@0 45 $this->matcher = $matcher;
Chris@0 46 $this->keyValue = $key_value;
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * {@inheritdoc}
Chris@0 51 */
Chris@0 52 public static function create(ContainerInterface $container) {
Chris@0 53 return new static(
Chris@0 54 $container->get('entity.autocomplete_matcher'),
Chris@0 55 $container->get('keyvalue')->get('entity_autocomplete')
Chris@0 56 );
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * Autocomplete the label of an entity.
Chris@0 61 *
Chris@0 62 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 63 * The request object that contains the typed tags.
Chris@0 64 * @param string $target_type
Chris@0 65 * The ID of the target entity type.
Chris@0 66 * @param string $selection_handler
Chris@0 67 * The plugin ID of the entity reference selection handler.
Chris@0 68 * @param string $selection_settings_key
Chris@0 69 * The hashed key of the key/value entry that holds the selection handler
Chris@0 70 * settings.
Chris@0 71 *
Chris@0 72 * @return \Symfony\Component\HttpFoundation\JsonResponse
Chris@0 73 * The matched entity labels as a JSON response.
Chris@0 74 *
Chris@0 75 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
Chris@0 76 * Thrown if the selection settings key is not found in the key/value store
Chris@0 77 * or if it does not match the stored data.
Chris@0 78 */
Chris@0 79 public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
Chris@0 80 $matches = [];
Chris@0 81 // Get the typed string from the URL, if it exists.
Chris@0 82 if ($input = $request->query->get('q')) {
Chris@0 83 $typed_string = Tags::explode($input);
Chris@0 84 $typed_string = Unicode::strtolower(array_pop($typed_string));
Chris@0 85
Chris@0 86 // Selection settings are passed in as a hashed key of a serialized array
Chris@0 87 // stored in the key/value store.
Chris@0 88 $selection_settings = $this->keyValue->get($selection_settings_key, FALSE);
Chris@0 89 if ($selection_settings !== FALSE) {
Chris@0 90 $selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt());
Chris@0 91 if ($selection_settings_hash !== $selection_settings_key) {
Chris@0 92 // Disallow access when the selection settings hash does not match the
Chris@0 93 // passed-in key.
Chris@0 94 throw new AccessDeniedHttpException('Invalid selection settings key.');
Chris@0 95 }
Chris@0 96 }
Chris@0 97 else {
Chris@0 98 // Disallow access when the selection settings key is not found in the
Chris@0 99 // key/value store.
Chris@0 100 throw new AccessDeniedHttpException();
Chris@0 101 }
Chris@0 102
Chris@0 103 $matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, $typed_string);
Chris@0 104 }
Chris@0 105
Chris@0 106 return new JsonResponse($matches);
Chris@0 107 }
Chris@0 108
Chris@0 109 }