You are here

public function VisibilityConfigurationForm::buildForm in Vertical Tabs Config 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/VisibilityConfigurationForm.php, line 48

Class

VisibilityConfigurationForm
Configure visibility for this site.

Namespace

Drupal\vertical_tabs_config\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $roles = user_roles();
  $ct_list = NodeType::loadMultiple();
  $vertical_tabs = vertical_tabs_config_vertical_tab_list();
  $conf = vertical_tabs_config_get_config();
  $form['desc'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t('For each content type, select which vertical tabs need to be hidden depending on roles.'),
  ];
  $form['content_types_config'] = [
    '#type' => 'vertical_tabs',
  ];
  foreach ($ct_list as $ct_machine_name => $obj) {
    $form['hide_' . $ct_machine_name] = [
      '#weight' => 5,
      '#type' => 'details',
      '#title' => $obj
        ->get('name'),
      '#group' => 'content_types_config',
    ];
    $form['hide_' . $ct_machine_name]['config'] = [
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => TRUE,
    ];
    $form['hide_' . $ct_machine_name]['config']['desc'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t("Select all vertical tabs that will be hidden for @ct. If you don't select any role, vertical tabs will be hidden to all roles.", [
        '@ct' => $obj
          ->get('name'),
      ]),
    ];
    $form['hide_' . $ct_machine_name]['config']['roles'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Hide only by role'),
      '#weight' => 5,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#group' => 'content_types_config_roles',
    ];
    $form['hide_' . $ct_machine_name]['config']['tabs'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Hidded vertical tabs'),
      '#weight' => 6,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#group' => 'content_types_config_tabs',
    ];
    foreach ($roles as $rid => $value) {
      $def = 0;
      if (isset($conf[$ct_machine_name]['roles']) && is_array($conf[$ct_machine_name]['roles'])) {
        if (in_array($rid, $conf[$ct_machine_name]['roles'])) {
          $def = 1;
        }
      }
      $form['hide_' . $ct_machine_name]['config']['roles']['role_' . $ct_machine_name . '_' . $rid] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Apply config for') . ' ' . $value
          ->get('label'),
        '#default_value' => $def,
        '#group' => 'vertical_tabs_roles',
      ];
    }
    foreach ($vertical_tabs as $vt_machine_name => $vt_human_name) {
      $def = isset($conf[$ct_machine_name][$vt_machine_name]) ? $conf[$ct_machine_name][$vt_machine_name] : 0;
      $form['hide_' . $ct_machine_name]['config']['tabs']['hide_' . $ct_machine_name . '_' . $vt_machine_name] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Hide') . ' ' . $vt_human_name,
        '#default_value' => $def,
        '#group' => 'vertical_tabs_hide',
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}