Chris@0: matcher = $matcher; Chris@0: $this->keyValue = $key_value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('entity.autocomplete_matcher'), Chris@0: $container->get('keyvalue')->get('entity_autocomplete') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Autocomplete the label of an entity. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The request object that contains the typed tags. Chris@0: * @param string $target_type Chris@0: * The ID of the target entity type. Chris@0: * @param string $selection_handler Chris@0: * The plugin ID of the entity reference selection handler. Chris@0: * @param string $selection_settings_key Chris@0: * The hashed key of the key/value entry that holds the selection handler Chris@0: * settings. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\JsonResponse Chris@0: * The matched entity labels as a JSON response. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * Thrown if the selection settings key is not found in the key/value store Chris@0: * or if it does not match the stored data. Chris@0: */ Chris@0: public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) { Chris@0: $matches = []; Chris@0: // Get the typed string from the URL, if it exists. Chris@0: if ($input = $request->query->get('q')) { Chris@0: $typed_string = Tags::explode($input); Chris@17: $typed_string = mb_strtolower(array_pop($typed_string)); Chris@0: Chris@0: // Selection settings are passed in as a hashed key of a serialized array Chris@0: // stored in the key/value store. Chris@0: $selection_settings = $this->keyValue->get($selection_settings_key, FALSE); Chris@0: if ($selection_settings !== FALSE) { Chris@0: $selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt()); Chris@0: if ($selection_settings_hash !== $selection_settings_key) { Chris@0: // Disallow access when the selection settings hash does not match the Chris@0: // passed-in key. Chris@0: throw new AccessDeniedHttpException('Invalid selection settings key.'); Chris@0: } Chris@0: } Chris@0: else { Chris@0: // Disallow access when the selection settings key is not found in the Chris@0: // key/value store. Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@0: Chris@0: $matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, $typed_string); Chris@0: } Chris@0: Chris@0: return new JsonResponse($matches); Chris@0: } Chris@0: Chris@0: }