You are here

public static function SuggestionHelper::alterElement in Autocomplete Search Suggestions 3.0.x

Same name and namespace in other branches
  1. 8.2 src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::alterElement()
  2. 8 src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::alterElement()

Search the form recursivley for the field and add the autocomplete route.

Parameters

array $form: Part or all of a form render array.

string $field_name: The field name to search for.

string $level: The current recursion level.

Return value

array An array of ngrams keys.

1 call to SuggestionHelper::alterElement()
suggestion_form_alter in ./suggestion.module
Implements hook_form_alter().

File

src/SuggestionHelper.php, line 28

Class

SuggestionHelper
Provides helper methods for suggestions.

Namespace

Drupal\suggestion

Code

public static function alterElement(array &$form, $field_name, $level = 1) {
  $suxs = FALSE;
  $types = [
    'search',
    'textfield',
  ];
  if ($level > self::MAX_LEVEL) {
    return FALSE;
  }
  foreach ($form as $key => &$element) {
    if ($key == $field_name && !empty($form[$key]['#type']) && in_array($form[$key]['#type'], $types)) {
      $form[$key]['#autocomplete_route_name'] = 'suggestion.autocomplete';
      $suxs = TRUE;
    }
    elseif (is_array($element)) {
      $suxs = self::alterElement($element, $field_name, $level + 1);
    }
    if ($suxs) {
      break;
    }
  }
  if ($suxs && $level == 1) {
    $form['#submit'][] = 'suggestion_surfer_submit';
  }
  return $suxs;
}