You are here

public function RealnameAutocompleteController::handleAutocomplete in Real Name 2.x

Same name and namespace in other branches
  1. 8 src/Controller/RealnameAutocompleteController.php \Drupal\realname\Controller\RealnameAutocompleteController::handleAutocomplete()

Autocomplete the label of an entity.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object that contains the typed tags.

string $target_type: The ID of the target entity type.

string $selection_handler: The plugin ID of the entity reference selection handler.

string $selection_settings_key: The hashed key of the key/value entry that holds the selection handler settings.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The matched entity labels as a JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown if the selection settings key is not found in the key/value store or if it does not match the stored data.

Overrides EntityAutocompleteController::handleAutocomplete

File

src/Controller/RealnameAutocompleteController.php, line 23

Class

RealnameAutocompleteController
Defines a route controller for entity autocomplete form elements.

Namespace

Drupal\realname\Controller

Code

public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
  if ($target_type != 'user') {
    return parent::handleAutocomplete($request, $target_type, $selection_handler, $selection_settings_key);
  }
  $matches = [];
  if ($input = $request->query
    ->get('q')) {
    $typed_string = Tags::explode($input);
    $typed_string = mb_strtolower(array_pop($typed_string));
    $selection_settings = $this->keyValue
      ->get($selection_settings_key, FALSE);
    if ($selection_settings !== FALSE) {
      $selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt());
      if ($selection_settings_hash !== $selection_settings_key) {
        throw new AccessDeniedHttpException('Invalid selection settings key.');
      }
    }
    else {
      throw new AccessDeniedHttpException();
    }
    $matches = $this
      ->getMatches($selection_settings, $typed_string);
  }
  return new JsonResponse($matches);
}