You are here

class CountryFieldManager in Country 8

Defines a class for country field management.

Hierarchy

Expanded class hierarchy of CountryFieldManager

1 file declares its use of CountryFieldManager
CountryAutocompleteController.php in src/Controller/CountryAutocompleteController.php
1 string reference to 'CountryFieldManager'
country.services.yml in ./country.services.yml
country.services.yml
1 service uses CountryFieldManager
country.field.manager in ./country.services.yml
Drupal\country\CountryFieldManager

File

src/CountryFieldManager.php, line 11

Namespace

Drupal\country
View source
class CountryFieldManager {

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

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

  /**
   * Get array of selectable countries.
   *
   * If some countries have been selected at the default field settings, allow
   * only those to be selectable. Else, check if any have been selected for the
   * field instance. If none, allow all available countries.
   *
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *  The field definition object.
   *
   * @return array
   *   The array of country names keyed by their ISO2 values.
   */
  public function getSelectableCountries(FieldDefinitionInterface $field_definition) {
    $field_definition_countries = $field_definition
      ->getSetting('selectable_countries');
    $field_storage_countries = $field_definition
      ->getFieldStorageDefinition()
      ->getSetting('selectable_countries');
    $countries = $this->countryManager
      ->getList();
    $allowed = !empty($field_definition_countries) ? $field_definition_countries : $field_storage_countries;
    return !empty($allowed) ? array_intersect_key($countries, $allowed) : $countries;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountryFieldManager::$countryManager protected property The country manager.
CountryFieldManager::getSelectableCountries public function Get array of selectable countries.
CountryFieldManager::__construct public function Constructs a new CountryAutocompleteController.