You are here

public static function SearchApiAutocomplete::processSearchApiAutocomplete in Search API Autocomplete 8

Adds Search API Autocomplete functionality to a form element.

Parameters

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

  • #search_id: The entity ID of the Search config entity.
  • #additional_data: (optional) Additional data to pass to the autocomplete callback as GET parameters.

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

array $complete_form: The complete form structure.

Return value

array The processed form element.

Throws

\InvalidArgumentException Thrown if the #search_id property is missing or invalid.

File

src/Element/SearchApiAutocomplete.php, line 52

Class

SearchApiAutocomplete
Provides a Search API Autocomplete form element.

Namespace

Drupal\search_api_autocomplete\Element

Code

public static function processSearchApiAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Require a search ID argument. If the #search_id key is not set on the
  // element, we can't load the Search entity so we should throw an exception.
  if (empty($element['#search_id'])) {
    throw new \InvalidArgumentException('Missing required "#search_id" parameter.');
  }

  /** @var \Drupal\search_api_autocomplete\SearchInterface $search */
  $search = Search::load($element['#search_id']);
  if (!$search) {
    $search_id = $element['#search_id'];
    throw new \InvalidArgumentException("Search entity with ID \"{$search_id}\" not found.");
  }
  $access = \Drupal::getContainer()
    ->get('search_api_autocomplete.helper')
    ->access($search, \Drupal::currentUser());
  $metadata = BubbleableMetadata::createFromRenderArray($element);
  $metadata
    ->merge(BubbleableMetadata::createFromObject($access))
    ->applyTo($element);
  if (!$access
    ->isAllowed()) {

    // Don't process if access isn't allowed.
    return $element;
  }

  // Add option defaults (in case of updates from earlier versions).
  $options = $search
    ->getOptions();
  $js_settings = [];
  if ($options['submit_button_selector'] != ':submit') {
    $js_settings['selector'] = $options['submit_button_selector'];
  }
  $delay = $search
    ->getOption('delay');
  if ($delay !== NULL) {
    $js_settings['delay'] = $delay;
  }
  if ($options['autosubmit']) {
    $js_settings['auto_submit'] = TRUE;
  }
  if ($options['min_length'] > 1) {
    $js_settings['min_length'] = $options['min_length'];
  }
  $element['#attached']['library'][] = 'search_api_autocomplete/search_api_autocomplete';
  if ($js_settings) {
    $element['#attached']['drupalSettings'] = [
      'search_api_autocomplete' => [
        $search
          ->id() => $js_settings,
      ],
    ];
  }
  $element['#autocomplete_route_name'] = 'search_api_autocomplete.autocomplete';
  $element['#autocomplete_route_parameters'] = [
    'search_api_autocomplete_search' => $search
      ->id(),
  ];
  if (!empty($element['#additional_data'])) {
    $element['#autocomplete_route_parameters'] += $element['#additional_data'];
  }
  $element['#attributes']['data-search-api-autocomplete-search'] = $search
    ->id();
  foreach ($search
    ->getSuggesters() as $suggester) {
    $suggester
      ->alterAutocompleteElement($element);
  }
  return $element;
}