You are here

class CountryFlagAutocompleteController in Flags 8

Returns autocomplete responses for countries.

Hierarchy

Expanded class hierarchy of CountryFlagAutocompleteController

File

flags_country/src/Controller/CountryFlagAutocompleteController.php, line 13

Namespace

Drupal\flags_country\Controller
View source
class CountryFlagAutocompleteController extends CountryAutocompleteController {

  /**
   * Returns response for the country name autocompletion.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object containing the search string.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing the autocomplete suggestions for countries.
   */
  public function autocomplete(Request $request, $entity_type, $bundle, $field_name) {

    /** @var \Drupal\Core\Render\Renderer $renderer */
    $renderer = \Drupal::service('renderer');
    $matches = array();
    $string = $request->query
      ->get('q');
    if ($string) {
      $field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
      $countries = \Drupal::service('country.field.manager')
        ->getSelectableCountries($field_definition);
      foreach ($countries as $iso2 => $country) {
        if (strpos(mb_strtolower($country), mb_strtolower($string)) !== FALSE) {
          $label = array(
            'country' => array(
              '#markup' => $country,
            ),
            'flag' => array(
              '#theme' => 'flags',
              '#code' => strtolower($iso2),
              '#source' => 'country',
            ),
          );
          $matches[] = array(
            'value' => $country,
            'label' => $renderer
              ->render($label),
          );
        }
      }
    }
    return new JsonResponse($matches);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountryAutocompleteController::$countryFieldManager protected property The country field manager.
CountryAutocompleteController::$countryManager protected property The country manager.
CountryAutocompleteController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CountryAutocompleteController::__construct public function Constructs a new CountryAutocompleteController.
CountryFlagAutocompleteController::autocomplete public function Returns response for the country name autocompletion. Overrides CountryAutocompleteController::autocomplete