You are here

public function LanguageAutocompleteController::autocomplete in Custom Language field 8

Returns response for the language autocomplete widget.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object containing the search string.

string $entity_type: The type of entity that owns the field.

string $bundle: The name of the bundle that owns the field.

string $field_name: The name of the field with the autocomplete widget.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.

See also

getMatches()

1 string reference to 'LanguageAutocompleteController::autocomplete'
languagefield.routing.yml in ./languagefield.routing.yml
languagefield.routing.yml

File

src/Controller/LanguageAutocompleteController.php, line 32

Class

LanguageAutocompleteController
Returns autocomplete responses for Languagefield.

Namespace

Drupal\languagefield\Controller

Code

public function autocomplete(Request $request, $entity_type, $bundle, $field_name) {
  $matches = [];
  $string = $request->query
    ->get('q');
  if ($string) {

    /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
    $field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
    $settings = $field_definition
      ->getSettings();
    $languages = CustomLanguageManager::allowedValues($settings);
    foreach ($languages as $language) {
      if (strpos(mb_strtolower($language), mb_strtolower($string)) !== FALSE) {
        $matches[] = [
          'value' => $language,
          'label' => $language,
        ];
      }
    }
  }
  return new JsonResponse($matches);
}