You are here

public function NameAutocompleteController::autocomplete in Name Field 8

Returns response for the name autocompletion.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object containing the search string.

string $field_name: The field name.

string $entity_type: The entity type.

string $bundle: The bundle.

string $component: The name component.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

See also

\Drupal\name\NameAutocomplete::getMatches()

1 string reference to 'NameAutocompleteController::autocomplete'
name.routing.yml in ./name.routing.yml
name.routing.yml

File

src/Controller/NameAutocompleteController.php, line 89

Class

NameAutocompleteController
Controller routines for name autocompletion routes.

Namespace

Drupal\name\Controller

Code

public function autocomplete(Request $request, $field_name, $entity_type, $bundle, $component) {
  $definitions = $this->entityFieldManager
    ->getFieldDefinitions($entity_type, $bundle);
  if (!isset($definitions[$field_name])) {
    throw new AccessDeniedHttpException();
  }
  $field_definition = $definitions[$field_name];
  $access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler($entity_type);
  if ($field_definition
    ->getType() != 'name' || !$access_control_handler
    ->fieldAccess('edit', $field_definition)) {
    throw new AccessDeniedHttpException();
  }
  $matches = $this->nameAutocomplete
    ->getMatches($field_definition, $component, $request->query
    ->get('q'));
  return new JsonResponse($matches);
}