annotate core/modules/system/src/Controller/EntityAutocompleteController.php @ 19:fa3358dc1485 tip

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