You are here

class NameAutocompleteController in Name Field 8

Controller routines for name autocompletion routes.

Hierarchy

Expanded class hierarchy of NameAutocompleteController

1 file declares its use of NameAutocompleteController
NameAutocompleteTest.php in tests/src/Kernel/NameAutocompleteTest.php

File

src/Controller/NameAutocompleteController.php, line 17

Namespace

Drupal\name\Controller
View source
class NameAutocompleteController implements ContainerInjectionInterface {

  /**
   * The name autocomplete helper class to find matching name values.
   *
   * @var \Drupal\name\NameAutocomplete
   */
  protected $nameAutocomplete;

  /**
   * Entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManager
   */
  protected $entityFieldManager;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs an NameAutocompleteController object.
   *
   * @param \Drupal\name\NameAutocomplete $name_autocomplete
   *   The name autocomplete helper class to find matching name values.
   * @param \Drupal\Core\Entity\EntityFieldManager $entityFieldManager
   *   The entity field manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity field manager.
   */
  public function __construct(NameAutocomplete $name_autocomplete, EntityFieldManager $entityFieldManager, EntityTypeManagerInterface $entityTypeManager) {
    $this->nameAutocomplete = $name_autocomplete;
    $this->entityFieldManager = $entityFieldManager;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('name.autocomplete'), $container
      ->get('entity_field.manager'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Returns response for the name autocompletion.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object containing the search string.
   * @param string $field_name
   *   The field name.
   * @param string $entity_type
   *   The entity type.
   * @param string $bundle
   *   The bundle.
   * @param string $component
   *   The name component.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing the autocomplete suggestions.
   *
   * @see \Drupal\name\NameAutocomplete::getMatches()
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NameAutocompleteController::$entityFieldManager protected property Entity field manager.
NameAutocompleteController::$entityTypeManager protected property Entity type manager.
NameAutocompleteController::$nameAutocomplete protected property The name autocomplete helper class to find matching name values.
NameAutocompleteController::autocomplete public function Returns response for the name autocompletion.
NameAutocompleteController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
NameAutocompleteController::__construct public function Constructs an NameAutocompleteController object.