You are here

public function AmswapConfigForm::buildForm in Admin Menu Swap 8

Same name and namespace in other branches
  1. 7.2 src/Form/AmswapConfigForm.php \Drupal\amswap\Form\AmswapConfigForm::buildForm()

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/AmswapConfigForm.php, line 62

Class

AmswapConfigForm
Class AmswapConfigForm.

Namespace

Drupal\amswap\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('amswap.amswapconfig');

  // Prepare role selector options.
  $roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple();

  // Add the first instructional option.
  $role_options = [
    '' => $this
      ->t('- Select a role -'),
  ];
  foreach ($roles as $role) {
    $role_options[$role
      ->id()] = $role
      ->label();
  }

  // Prepare menu selector options.
  $menus = $this->entityTypeManager
    ->getStorage('menu')
    ->loadMultiple();

  // Add the first instructional option.
  $menu_options = [
    '' => $this
      ->t('- Select a menu -'),
  ];
  foreach ($menus as $menu) {
    $menu_options[$menu
      ->id()] = $menu
      ->label();
  }
  $role_menu_pairs = $config
    ->get('role_menu_pairs');
  $num_pairs = $form_state
    ->get('num_pairs');
  $num_pairs = $num_pairs ? $num_pairs : 1;

  // Use the larger of pairs saved or pairs added using the button.
  $num_pairs_in_form = count($role_menu_pairs) > $num_pairs ? count($role_menu_pairs) : $num_pairs;
  $form_state
    ->set('num_pairs', $num_pairs_in_form);
  for ($i = 0; $i < $num_pairs_in_form; $i++) {
    $form['role_menu_pairs'][$i] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Role-Menu Pair @i', [
        '@i' => $i + 1,
      ]),
    ];
    $role = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['role'] : NULL;
    $form['role_menu_pairs'][$i]['pair-' . $i . '-role'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Role'),
      '#description' => $this
        ->t('Select a role.'),
      '#default_value' => $role,
      '#options' => $role_options,
    ];
    $menu = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['menu'] : NULL;
    $form['role_menu_pairs'][$i]['pair-' . $i . '-menu'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Menu'),
      '#description' => $this
        ->t('Select which menu should be displayed for that role.'),
      '#default_value' => $menu,
      '#options' => $menu_options,
    ];
    $form['role_menu_pairs'][$i]['pair-' . $i . '-delete'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Remove @i', [
        '@i' => $i + 1,
      ]),
      '#submit' => [
        '::deletePair',
      ],
      '#attributes' => [
        'pair_num' => $i,
      ],
    ];
  }
  $form['add_pair'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another role-menu pair'),
    '#submit' => [
      '::addPair',
    ],
  ];
  return parent::buildForm($form, $form_state);
}