You are here

protected function AdministrativeArea::getCurrentCountry in Address 8

Gets the currently active country code.

The country source determines where to look for the country code. It can either be predefined, in which case we simply return the current value of the static country code (via form values or configuration). We can look for the country via a Views argument, in which case we determine the current value of the argument. Or we can get the country from another exposed filter, in which case we look in the form values to find the current country code from the other filter.

Return value

string The 2-letter country code.

3 calls to AdministrativeArea::getCurrentCountry()
AdministrativeArea::exposedInfo in src/Plugin/views/filter/AdministrativeArea.php
Tell the renderer about our exposed form. This only needs to be overridden for particularly complex forms. And maybe not even then.
AdministrativeArea::getValueOptions in src/Plugin/views/filter/AdministrativeArea.php
Child classes should be used to override this function and set the 'value options', unless 'options callback' is defined as a valid function or static public method to generate these values.
AdministrativeArea::valueForm in src/Plugin/views/filter/AdministrativeArea.php
Options form subform for setting options.

File

src/Plugin/views/filter/AdministrativeArea.php, line 468

Class

AdministrativeArea
Filter by administrative area.

Namespace

Drupal\address\Plugin\views\filter

Code

protected function getCurrentCountry() {
  $this->currentCountryCode = '';
  switch ($this
    ->getCountrySource()) {
    case 'argument':
      $country_argument = $this->view->display_handler
        ->getHandler('argument', $this->options['country']['country_argument_id']);
      if (!empty($country_argument)) {
        $this->currentCountryCode = $country_argument
          ->getValue();
      }
      break;
    case 'filter':
      $country_filter = $this->view->display_handler
        ->getHandler('filter', $this->options['country']['country_filter_id']);
      if (!empty($country_filter) && !empty($this->formState)) {
        $input = $this->formState
          ->getUserInput();
        $country_filter_identifier = $country_filter->options['expose']['identifier'];
        if (!empty($input[$country_filter_identifier])) {
          if (is_array($input[$country_filter_identifier])) {

            // @todo Maybe the config validation should prevent multi-valued
            // country filters. For now, we only provide administrative area
            // options if a single country is selected.
            if (count($input[$country_filter_identifier]) == 1) {
              $this->currentCountryCode = array_shift($input[$country_filter_identifier]);
            }
          }
          else {
            $this->currentCountryCode = $input[$country_filter_identifier];
          }
        }
      }
      break;
    case 'static':
      if (!empty($this->formState)) {

        // During filter configuration validation, we still need to know the
        // current country code, but the values won't yet be saved into the
        // ones accessible via FormStateInterface::getValue(). So, directly
        // inspect the user input instead of the official form values.
        $input = $this->formState
          ->getUserInput();
        if (!empty($input['options']['country']['country_static_code'])) {
          $form_input_country_code = $input['options']['country']['country_static_code'];
        }
      }
      $this->currentCountryCode = !empty($form_input_country_code) ? $form_input_country_code : $this->options['country']['country_static_code'];
      break;
  }

  // Since the country code can come from all sorts of non-validated user
  // input (e.g. GET parameters) and since it might be 'All', ensure we've
  // got a valid country code before we proceed. Other code in this
  // filter (and especially upstream in the AddressFormatRepository and
  // others) will explode if passed an invalid country code.
  if (!empty($this->currentCountryCode)) {
    $all_countries = $this->countryRepository
      ->getList();
    if (empty($all_countries[$this->currentCountryCode])) {
      $this->currentCountryCode = '';
    }
  }
  return $this->currentCountryCode;
}