You are here

public static function Linkit::processLinkitAutocomplete in Linkit 8.4

Same name and namespace in other branches
  1. 8.5 src/Element/Linkit.php \Drupal\linkit\Element\Linkit::processLinkitAutocomplete()

Adds linkit custom autocomplete functionality to elements.

Instead of using the core autocomplete, we use our own.

See also

\Drupal\Core\Render\Element\FormElement::processAutocomplete

File

src/Element/Linkit.php, line 61
Contains \Drupal\linkit\Element\Linkit.

Class

Linkit
Provides a form element for linkit.

Namespace

Drupal\linkit\Element

Code

public static function processLinkitAutocomplete(&$element, FormStateInterface $form_state, &$complete_form) {
  $url = NULL;
  $access = FALSE;
  if (!empty($element['#autocomplete_route_name'])) {
    $parameters = isset($element['#autocomplete_route_parameters']) ? $element['#autocomplete_route_parameters'] : array();
    $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)
      ->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-linkit-autocomplete';
      $metadata
        ->addAttachments([
        'library' => [
          'linkit/linkit.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;
}