You are here

public function CountryStateCityWidget::getCities in Country, State and City Fields 8

1 call to CountryStateCityWidget::getCities()
CountryStateCityWidget::formElement in src/Plugin/Field/FieldWidget/CountryStateCityWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/CountryStateCityWidget.php, line 143

Class

CountryStateCityWidget
Plugin implementation of the 'country_state_city_widget' widget.

Namespace

Drupal\country_state_city\Plugin\Field\FieldWidget

Code

public function getCities($state_id) {
  if ($state_id) {
    $query = $this->entityTypeManager
      ->getStorage('citylist')
      ->getQuery()
      ->condition('state_id', $state_id, '=')
      ->sort('name', 'asc');
    $ids = $query
      ->execute();
    $cities = [];
    if (count($ids) == 1) {
      $result = $this->entityTypeManager
        ->getStorage('citylist')
        ->load(key($ids));
      $cities[$result
        ->id()] = $result
        ->getName();
    }
    elseif (count($ids) > 1) {
      $results = $this->entityTypeManager
        ->getStorage('citylist')
        ->loadMultiple($ids);
      foreach ($results as $result) {
        $cities[$result
          ->id()] = $result
          ->getName();
      }
    }
    return $cities;
  }
}