You are here

public function SwitchPageThemeSettingForm::buildForm in Switch Page Theme 8

Same name and namespace in other branches
  1. 8.3 src/Form/SwitchPageThemeSettingForm.php \Drupal\switch_page_theme\Form\SwitchPageThemeSettingForm::buildForm()
  2. 8.2 src/Form/SwitchPageThemeSettingForm.php \Drupal\switch_page_theme\Form\SwitchPageThemeSettingForm::buildForm()

Implements admin settings form.

Parameters

array $form: From render array.

\Drupal\Core\Form\FormStateInterface $form_state: Current state of form.

Overrides ConfigFormBase::buildForm

File

src/Form/SwitchPageThemeSettingForm.php, line 120

Class

SwitchPageThemeSettingForm
Configuration page for Switch page theme settings.

Namespace

Drupal\switch_page_theme\Form

Code

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

  // Fetch configurations if saved.
  $config = $this
    ->config('switch_page_theme.settings');

  // Instructions.
  $availableSettings = $this
    ->t('Roles');
  if ($this->moduleHandler
    ->moduleExists('domain')) {
    $availableSettings .= '/' . $this
      ->t('Domains');
  }
  if ($this->languageManager
    ->isMultilingual() || $this->moduleHandler
    ->moduleExists('language')) {
    $availableSettings .= '/' . $this
      ->t('Languages');
  }
  $form['desc'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('<b>Enabled:</b> Rule will work only if checkbox is checked.<br><b>Pages:</b> Enter one path per line. The "*" character is a wildcard. Example paths are "/node/1" for an individual piece of content or "/node/*" for every piece of content. "@front" is the front page.<br><b>Theme key:</b> Enter the theme key value to access the selected theme, e.g. Pass AAABBBCCC in the theme key field to access the theme on URL?theme_key=AAABBBCCC.<br><b>@availableSettings:</b> Select none to allow all.<br><br>Theme with highest weight will be applied on the page.', [
      '@availableSettings' => $availableSettings,
      '@front' => '<front>',
    ]),
  ];

  // Create headers for table.
  $header = [
    $this
      ->t('Enabled'),
    $this
      ->t('pages'),
    $this
      ->t('Theme Key'),
    $this
      ->t('Themes'),
    $this
      ->t('Roles'),
  ];
  if ($this->moduleHandler
    ->moduleExists('domain')) {
    $header[] = $this
      ->t('Domain');
  }
  if ($this->languageManager
    ->isMultilingual() || $this->moduleHandler
    ->moduleExists('language')) {
    $header[] = $this
      ->t('Language');
  }
  array_push($header, $this
    ->t('Operation'), $this
    ->t('Weight'));

  // Multi value table form.
  $form['spt_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#empty' => $this
      ->t('There are no items yet. Add an item.', []),
    '#prefix' => '<div id="spt-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'spt_table-order-weight',
      ],
    ],
  ];

  // Available themes.
  $themes = $this->themeHandler
    ->listInfo();
  $themeNames[''] = '--Select--';
  foreach ($themes as $key => $value) {
    $themeNames[$key] = $value->info['name'];
  }

  // Set table values on Add/Remove or on page load.
  $spt_table = $form_state
    ->get('spt_table');
  if (empty($spt_table)) {

    // Set data from configuration on page load.
    // Set empty element if no configurations are set.
    if (!empty($config
      ->get('spt_table'))) {
      $spt_table = $config
        ->get('spt_table');
      $form_state
        ->set('spt_table', $spt_table);
    }
    else {
      $spt_table = [
        '',
      ];
      $form_state
        ->set('spt_table', $spt_table);
    }
  }

  // Provide ability to remove first element.
  // Set Pages & Theme to required based on condition.
  $required = TRUE;
  if (isset($spt_table['removed']) && $spt_table['removed']) {

    // Not required if first element is empty.
    $first_element = reset($spt_table);
    $req_roles = FALSE;
    if ($first_element['pages'] == '' && $first_element['theme_key'] == '' && $first_element['theme'] == '' && $first_element['status'] == '') {
      foreach ($first_element['roles'] as $key => $value) {
        if ($value != 0) {
          $req_roles = TRUE;
        }
      }
      if (!$req_roles) {
        $required = FALSE;
      }
    }
    unset($spt_table['removed']);
  }

  // Don't allow to add multiple elements after all rows are removed.
  if (count($spt_table) > 1) {
    $required = TRUE;
  }

  // Create row for table.
  foreach ($spt_table as $i => $value) {
    $form['spt_table'][$i]['#attributes']['class'][] = 'draggable';
    $form['spt_table'][$i]['status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Status'),
      '#title_display' => 'invisible',
      '#default_value' => isset($value['status']) ? $value['status'] : NULL,
    ];
    $form['spt_table'][$i]['pages'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Pages'),
      '#title_display' => 'invisible',
      '#required' => $required,
      '#cols' => '5',
      '#rows' => '5',
      '#default_value' => isset($value['pages']) ? $value['pages'] : [],
    ];
    $form['spt_table'][$i]['theme_key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Theme Key'),
      '#title_display' => 'invisible',
      '#size' => 20,
      '#default_value' => isset($value['theme_key']) ? $value['theme_key'] : [],
    ];
    $form['spt_table'][$i]['theme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Theme'),
      '#title_display' => 'invisible',
      '#options' => $themeNames,
      '#required' => $required,
      '#default_value' => isset($value['theme']) ? $value['theme'] : [],
    ];
    $form['spt_table'][$i]['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Roles'),
      '#title_display' => 'invisible',
      '#options' => user_role_names(),
      '#default_value' => isset($value['roles']) ? $value['roles'] : [],
    ];

    // Add Domains if domain module is available.
    $domainNames = [];
    if ($this->moduleHandler
      ->moduleExists('domain')) {
      foreach ($this->domainStorage
        ->loadMultiple() as $domain_key => $domain) {
        $domainNames[$domain_key] = $domain
          ->getHostname();
      }
      $form['spt_table'][$i]['domain'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Domain'),
        '#title_display' => 'invisible',
        '#options' => $domainNames,
        '#default_value' => isset($value['domain']) ? $value['domain'] : [],
      ];
    }

    // Add Language if site is multilingual.
    if ($this->languageManager
      ->isMultilingual() || $this->moduleHandler
      ->moduleExists('language')) {
      $langNames = [];
      foreach ($this->languageManager
        ->getLanguages() as $langkey => $langvalue) {
        $langNames[$langkey] = $langvalue
          ->getName();
      }
      $form['spt_table'][$i]['language'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Language'),
        '#title_display' => 'invisible',
        '#options' => $langNames,
        '#default_value' => isset($value['language']) ? $value['language'] : [],
      ];
    }
    $form['spt_table'][$i]['remove'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Remove'),
      '#name' => "remove-" . $i,
      '#submit' => [
        '::removeElement',
      ],
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => '::removeCallback',
        'wrapper' => 'spt-fieldset-wrapper',
      ],
      '#index_position' => $i,
    ];

    // TableDrag: Weight column element.
    $form['spt_table'][$i]['weight'] = [
      '#type' => 'weight',
      '#title_display' => 'invisible',
      '#default_value' => isset($value['weight']) ? $value['weight'] : [],
      '#attributes' => [
        'class' => [
          'spt_table-order-weight',
        ],
      ],
    ];
  }
  $form['add_name'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add one more'),
    '#submit' => [
      '::addOne',
    ],
    '#ajax' => [
      'callback' => '::addmoreCallback',
      'wrapper' => 'spt-fieldset-wrapper',
    ],
  ];
  $form_state
    ->setCached(FALSE);
  return parent::buildForm($form, $form_state);
}