You are here

public function SwitchForm::addSwitchFields in Domain Access 8

Helper to add switch fields to form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state array.

1 call to SwitchForm::addSwitchFields()
SwitchForm::buildForm in domain_config_ui/src/Form/SwitchForm.php
Form constructor.

File

domain_config_ui/src/Form/SwitchForm.php, line 106

Class

SwitchForm
Class SwitchForm.

Namespace

Drupal\domain_config_ui\Form

Code

public function addSwitchFields(array $form, FormStateInterface $form_state) {

  // Create fieldset to group domain fields.
  $form['domain_config_ui'] = [
    '#type' => 'fieldset',
    '#title' => 'Domain Configuration',
    '#weight' => -10,
  ];

  // Add domain switch select field.
  if ($selected_domain_id = $this->domainConfigUiManager
    ->getSelectedDomainId()) {
    $selected_domain = $this->domainStorage
      ->load($selected_domain_id);
  }

  // Get the form options.
  $form['domain_config_ui']['domain'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Domain'),
    '#options' => $this
      ->getDomainOptions(),
    '#default_value' => !empty($selected_domain) ? $selected_domain
      ->id() : '',
    '#ajax' => [
      'callback' => '::switchCallback',
    ],
  ];

  // Add language select field. Domain Config does not rely on core's Config
  // Translation module, so we set our own permission.
  $languages = $this->languageManager
    ->getLanguages();
  if (count($languages) > 1 && $this
    ->currentUser()
    ->hasPermission('translate domain configuration')) {
    $language_options = [
      '' => $this
        ->t('Default'),
    ];
    foreach ($languages as $id => $language) {
      if (!$language
        ->isLocked()) {
        $language_options[$id] = $language
          ->getName();
      }
    }
    $form['domain_config_ui']['language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Language'),
      '#options' => $language_options,
      '#default_value' => $this->domainConfigUiManager
        ->getSelectedLanguageId(),
      '#ajax' => [
        'callback' => '::switchCallback',
      ],
    ];
    $form['domain_config_ui']['help'] = [
      '#markup' => $this
        ->t('Changing the domain or language will load its active configuration.'),
    ];
  }
  else {
    $form['domain_config_ui']['help'] = [
      '#markup' => $this
        ->t('Changing the domain will load its active configuration.'),
    ];
  }

  // @TODO: Add cache contexts to form?
  return $form;
}