You are here

public function AdminSettingsForm::buildForm in Role Theme Switcher 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/AdminSettingsForm.php, line 65

Class

AdminSettingsForm
Provides the theme switcher configuration form.

Namespace

Drupal\role_theme_switcher\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $themes = $this
    ->getThemeList();
  $form['role_theme_switcher'] = [
    '#type' => 'table',
    '#title' => $this
      ->t('Role processing order'),
    '#header' => [
      $this
        ->t('Role name'),
      $this
        ->t('Selected Theme'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('There are no items yet. Add roles.'),
    '#tabledrag' => [
      [
        'table_id' => 'edit-role-theme-switcher',
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'role-order-weight',
      ],
    ],
  ];
  $roles = $this
    ->getUserRoles();
  foreach ($roles as $rid => $role) {
    $form['role_theme_switcher'][$rid]['#attributes']['class'][] = 'draggable';
    $form['role_theme_switcher'][$rid]['#weight'] = $role['weight'];

    // Role name col.
    $form['role_theme_switcher'][$rid]['role'] = [
      '#plain_text' => $role['name'],
    ];

    // Theme col.
    $form['role_theme_switcher'][$rid]['theme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select Theme'),
      '#options' => $themes,
      '#empty_option' => $this
        ->t('Default theme'),
      '#empty_value' => '',
      '#default_value' => $role['theme'],
    ];

    // Weight col.
    $form['role_theme_switcher'][$rid]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $role['name'],
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $role['weight'],
      '#attributes' => [
        'class' => [
          'role-order-weight',
        ],
      ],
      '#delta' => 50,
    ];
  }
  return parent::buildForm($form, $form_state);
}