You are here

public function DomainThemeSwitchConfigForm::buildForm in Domain Theme Switch 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/DomainThemeSwitchConfigForm.php, line 103

Class

DomainThemeSwitchConfigForm
Class DomainThemeSwitchConfigForm.

Namespace

Drupal\domain_theme_switch\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('domain_theme_switch.settings');
  $defaultSiteTheme = $this
    ->config('system.theme')
    ->get('default');
  $defaultAdminTheme = $this
    ->config('system.theme')
    ->get('admin');
  $themeNames = $this
    ->getThemeList();
  $domains = $this->entityTypeManager
    ->getStorage('domain')
    ->loadMultiple();
  foreach ($domains as $domain) {
    $domainId = $domain
      ->id();
    $hostname = $domain
      ->get('name');
    $form[$domainId] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Select Theme for "@domain"', [
        '@domain' => $hostname,
      ]),
    ];
    $form[$domainId][$domainId . '_site'] = [
      '#title' => $this
        ->t('Site theme for domain'),
      '#type' => 'select',
      '#options' => $themeNames,
      '#default_value' => NULL !== $config
        ->get($domainId . '_site') ? $config
        ->get($domainId . '_site') : $defaultSiteTheme,
    ];
    $form[$domainId][$domainId . '_admin'] = [
      '#title' => $this
        ->t('Admin theme for domain'),
      '#suffix' => $this
        ->t('Change permission to allow domain admin theme @link.', [
        '@link' => Link::fromTextAndUrl($this
          ->t('change permission'), Url::fromRoute('user.admin_permissions', [], [
          'fragment' => 'module-domain_theme_switch',
        ]))
          ->toString(),
      ]),
      '#type' => 'select',
      '#options' => $themeNames,
      '#default_value' => NULL !== $config
        ->get($domainId . '_admin') ? $config
        ->get($domainId . '_admin') : $defaultAdminTheme,
    ];
  }
  if (count($domains) === 0) {
    $form['domain_theme_switch_message'] = [
      '#markup' => $this
        ->t('Zero domain records found. Please @link to create the domain.', [
        '@link' => Link::fromTextAndUrl($this
          ->t('click here'), Url::fromRoute('domain.admin'))
          ->toString(),
      ]),
    ];
    return $form;
  }
  return parent::buildForm($form, $form_state);
}