You are here

class CountryAutocompleteController in Country 8

Returns autocomplete responses for countries.

Hierarchy

Expanded class hierarchy of CountryAutocompleteController

File

src/Controller/CountryAutocompleteController.php, line 16

Namespace

Drupal\country\Controller
View source
class CountryAutocompleteController implements ContainerInjectionInterface {

  /**
   * The country manager.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface
   */
  protected $countryManager;

  /**
   * The country field manager.
   *
   * @var \Drupal\country\CountryFieldManager
   */
  protected $countryFieldManager;

  /**
   * Constructs a new CountryAutocompleteController.
   *
   * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
   *   The country manager.
   * @param \Drupal\country\CountryFieldManager $country_field_manager
   *   The country field manager.
   */
  public function __construct(CountryManagerInterface $country_manager, CountryFieldManager $country_field_manager) {
    $this->countryManager = $country_manager;
    $this->countryFieldManager = $country_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('country_manager'), $container
      ->get('country.field.manager'));
  }

  /**
   * Returns response for the country name autocompletion.
   *
   * @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 $field_name
   *   The name of the field.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing the autocomplete suggestions for countries.
   */
  public function autocomplete(Request $request, $entity_type, $bundle, $field_name) {
    $matches = [];
    $string = $request->query
      ->get('q');
    if ($string) {

      // Check if the bundle is global - in that case we need all countries.
      if ($bundle == 'global') {
        $countries = $this->countryManager
          ->getList();
      }
      else {

        /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
        $field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
        $countries = $this->countryFieldManager
          ->getSelectableCountries($field_definition);
      }
      foreach ($countries as $iso2 => $country) {
        if (strpos(mb_strtolower($country), mb_strtolower($string)) !== FALSE) {
          $matches[] = [
            'value' => $country,
            'label' => $country,
          ];
        }
      }
    }
    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::autocomplete public function Returns response for the country name autocompletion.
CountryAutocompleteController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CountryAutocompleteController::__construct public function Constructs a new CountryAutocompleteController.