function FormElementBase::processAutocomplete

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Render/Element/FormElementBase.php \Drupal\Core\Render\Element\FormElementBase::processAutocomplete()

Adds autocomplete functionality to elements.

This sets up autocomplete functionality for elements with an #autocomplete_route_name property, using the #autocomplete_route_parameters and #autocomplete_query_parameters properties if present.

For example, suppose your autocomplete route name is 'my_module.autocomplete' and its path is '/my_module/autocomplete/{a}/{b}'. In a form array, you would create a text field with properties:


'#autocomplete_route_name' => 'my_module.autocomplete',
'#autocomplete_route_parameters' => ['a' => $some_key, 'b' => $some_id],

If the user types "keywords" in that field, the full path called would be: 'my_module_autocomplete/$some_key/$some_id?q=keywords'

Parameters

array $element: The form element to process. Properties used:

  • #autocomplete_route_name: A route to be used as callback URL by the autocomplete JavaScript library.
  • #autocomplete_route_parameters: The parameters to be used in conjunction with the route name.
  • #autocomplete_query_parameters: The parameters to be used in query string

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The form element.

1 call to FormElementBase::processAutocomplete()
FormElement::processAutocomplete in core/lib/Drupal/Core/Render/Element/FormElement.php
1 method overrides FormElementBase::processAutocomplete()
FormElement::processAutocomplete in core/lib/Drupal/Core/Render/Element/FormElement.php

File

core/lib/Drupal/Core/Render/Element/FormElementBase.php, line 190

Class

FormElementBase
Provides a base class for form element plugins.

Namespace

Drupal\Core\Render\Element

Code

public static function processAutocomplete(&$element, FormStateInterface $form_state, &$complete_form) {
    $url = NULL;
    $access = FALSE;
    if (!empty($element['#autocomplete_route_name'])) {
        $parameters = $element['#autocomplete_route_parameters'] ?? [];
        $options = [];
        if (!empty($element['#autocomplete_query_parameters'])) {
            $options['query'] = $element['#autocomplete_query_parameters'];
        }
        $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters, $options)->toString(TRUE);
        
        /** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
        $access_manager = \Drupal::service('access_manager');
        $access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $parameters, \Drupal::currentUser(), TRUE);
    }
    if ($access) {
        $metadata = BubbleableMetadata::createFromRenderArray($element);
        if ($access->isAllowed()) {
            $element['#attributes']['class'][] = 'form-autocomplete';
            $metadata->addAttachments([
                'library' => [
                    'core/drupal.autocomplete',
                ],
            ]);
            // Provide a data attribute for the JavaScript behavior to bind to.
            $element['#attributes']['data-autocomplete-path'] = $url->getGeneratedUrl();
            $metadata = $metadata->merge($url);
        }
        $metadata->merge(BubbleableMetadata::createFromObject($access))
            ->applyTo($element);
    }
    return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.