You are here

class LanguageAutocompleteController in Custom Language field 8

Returns autocomplete responses for Languagefield.

Hierarchy

Expanded class hierarchy of LanguageAutocompleteController

File

src/Controller/LanguageAutocompleteController.php, line 13

Namespace

Drupal\languagefield\Controller
View source
class LanguageAutocompleteController {

  /**
   * Returns response for the language autocomplete widget.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object containing the search string.
   * @param string $entity_type
   *   The type of entity that owns the field.
   * @param string $bundle
   *   The name of the bundle that owns the field.
   * @param string $field_name
   *   The name of the field with the autocomplete widget.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing the autocomplete suggestions.
   *
   * @see getMatches()
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageAutocompleteController::autocomplete public function Returns response for the language autocomplete widget.