You are here

class IpLanguageNegotiationForm in IP Language Negotiation 8

IP language negotiation form.

Hierarchy

Expanded class hierarchy of IpLanguageNegotiationForm

1 string reference to 'IpLanguageNegotiationForm'
ip_language_negotiation.routing.yml in ./ip_language_negotiation.routing.yml
ip_language_negotiation.routing.yml

File

src/Form/IpLanguageNegotiationForm.php, line 12

Namespace

Drupal\ip_language_negotiation\Form
View source
class IpLanguageNegotiationForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ip_language_negotiation_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $countries = \Drupal::service('country_manager')
      ->getList();
    $languages = \Drupal::languageManager()
      ->getLanguages();
    $language_default = \Drupal::languageManager()
      ->getDefaultLanguage();
    $settings = \Drupal::config('ip_language_negotiation.settings')
      ->get('ip_language_negotiation_countries') ?: [];
    $ip2country_settings_link = Url::fromRoute('ip2country.settings', [
      'fragment' => 'edit-ip2country-debug-preferences',
    ])
      ->toString();
    $form['intro'] = [
      '#markup' => '<p>' . t('Use the interface below to select the default language per country. You only have to set the exceptions, because the default language will be used as fall-back. You can use the <a href="@url">Debug preferences</a> to test the module.', [
        '@url' => $ip2country_settings_link,
      ]) . '</p>',
    ];

    // Remove the default language.
    unset($languages[$language_default
      ->getId()]);

    // Build languages options array.
    $language_options = [
      '' => t('Default (@default_language)', [
        '@default_language' => $language_default
          ->getName(),
      ]),
    ];
    foreach ($languages as $language) {
      $language_options[$language
        ->getId()] = $language
        ->getName();
    }
    $letter = '';
    foreach ($countries as $country_code => $country) {

      // Remove accents so we can sort countries correctly.
      $current_letter = iconv('UTF-8', 'ASCII//TRANSLIT', mb_substr($country, 0, 1));
      if ($letter != $current_letter) {
        $letter = $current_letter;
        if (empty($form['ip_language_letter_' . $letter])) {
          $form['ip_language_letter_' . $letter] = [
            '#type' => 'fieldset',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#title' => t('Countries with the letter %letter', [
              '%letter' => $letter,
            ]),
          ];
        }
      }
      $form['ip_language_letter_' . $letter][$country_code] = [
        '#type' => 'radios',
        '#options' => $language_options,
        '#title' => $country,
        '#default_value' => '',
      ];
      if (!empty($settings[$country_code])) {
        $form['ip_language_letter_' . $letter][$country_code]['#default_value'] = $settings[$country_code];
        $form['ip_language_letter_' . $letter]['#collapsed'] = FALSE;
      }
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Exclude unnecessary elements.
    $form_state
      ->cleanValues();
    \Drupal::configFactory()
      ->getEditable('ip_language_negotiation.settings')
      ->set('ip_language_negotiation_countries', $form_state
      ->getValues())
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
IpLanguageNegotiationForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
IpLanguageNegotiationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
IpLanguageNegotiationForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.