You are here

public function AmswapConfigForm::buildForm in Admin Menu Swap 7.2

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

File

src/Form/AmswapConfigForm.php, line 34

Class

AmswapConfigForm
Class AmswapConfigForm.

Namespace

Drupal\amswap\Form

Code

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

  // kint($form, '$form');
  // kint($form_state, '$form_state');
  $config = $this
    ->config('amswap.amswapconfig');

  // @deprecated Old text field
  // $form['role_menu_pairs'] = [
  //   '#type' => 'textarea',
  //   '#title' => $this->t('Role & menu pairs'),
  //   '#description' => $this->t('Roles and their associated menus. Eg "owner:owner-menu; ..."'),
  //   '#default_value' => $config->get('role_menu_pairs'),
  // ];
  $role_menu_pairs = $config
    ->get('role_menu_pairs');

  // kint(['test'], '$role_menu_pairs');
  // var_dump($role_menu_pairs);
  $num_pairs = $form_state
    ->get('num_pairs');
  $num_pairs = $num_pairs ? $num_pairs : 1;

  // var_dump($num_pairs);
  // 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 + 1)),
    ];
    $role = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['role'] : NULL;
    $form['role_menu_pairs'][$i]['pair-' . $i . '-role'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Role'),
      '#description' => $this
        ->t('The machine name of a role. Eg &quot;owner&quot;'),
      '#default_value' => $role,
    ];
    $menu = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['menu'] : NULL;
    $form['role_menu_pairs'][$i]['pair-' . $i . '-menu'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Menu'),
      '#description' => $this
        ->t('The machine name of menu that should be displayed for that role. Eg &quot;owner&quot;'),
      '#default_value' => $menu,
    ];
    $form['role_menu_pairs'][$i]['pair-' . $i . '-delete'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Remove ' . ($i + 1)),
      '#submit' => [
        '::amswap_delete_pair',
      ],
      '#attributes' => [
        'pair_num' => $i,
      ],
    ];
  }
  $form['add_pair'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another role-menu pair'),
    '#submit' => [
      '::amswap_add_pair',
    ],
  ];
  return parent::buildForm($form, $form_state);
}