You are here

public function ConfigPermListForm::buildForm in Custom Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Form/ConfigPermListForm.php \Drupal\config_perms\Form\ConfigPermListForm::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 FormInterface::buildForm

File

src/Form/ConfigPermListForm.php, line 27

Class

ConfigPermListForm
Class ConfigPermListForm.

Namespace

Drupal\config_perms\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['perms'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Custom Permissions'),
    '#description' => '<p>' . $this
      ->t("Please note that the order in which permissions are granted are as follows:") . '</p>' . "<ul>\n       <li>" . $this
      ->t("Custom permissions only support internal paths") . "</li>\n\n       <li>" . $this
      ->t("User 1 still maintains full control") . "</li>\n\n       <li>" . $this
      ->t("Remove the permission 'Administer site configuration' from roles you wish to give access to only specified custom site configuration permissions") . "</li>\n\n      </ul>",
    '#collapsible' => 1,
    '#collapsed' => 0,
  ];
  $perms = CustomPermsEntity::loadMultiple();
  $header = [
    $this
      ->t('Enabled'),
    $this
      ->t('Name'),
    $this
      ->t('Path(s)'),
  ];
  $form['perms']['local'] = [
    '#type' => 'table',
    '#header' => $header,
    '#prefix' => '<div id="config_perms-wrapper">',
    '#suffix' => '</div>',
  ];
  foreach ($perms as $key => $perm) {
    $form['perms']['local'][$key] = [
      '#tree' => TRUE,
    ];
    $form['perms']['local'][$key]['status'] = [
      '#type' => 'checkbox',
      '#default_value' => $perm
        ->status(),
    ];
    $form['perms']['local'][$key]['name'] = [
      '#type' => 'textfield',
      '#default_value' => $perm
        ->label(),
      '#size' => 30,
    ];
    $form['perms']['local'][$key]['path'] = [
      '#type' => 'textarea',
      '#default_value' => $perm
        ->getPath(),
      '#size' => 50,
      '#rows' => 1,
    ];

    // Delete link.
    $url_object = Url::fromUri('internal:/admin/structure/custom_perms_entity/' . $perm
      ->id() . '/delete');
    $delete_link = \Drupal::l($this
      ->t('Delete'), $url_object);
    $form['perms']['local'][$key]['delete'] = [
      '#type' => 'item',
      '#markup' => $delete_link,
    ];
    $form['perms']['local'][$key]['id'] = [
      '#type' => 'hidden',
      '#default_value' => $perm
        ->id(),
    ];
  }
  $num_new = $form_state
    ->getValue('num_new');
  if (empty($num_new)) {
    $form_state
      ->setValue('num_new', '0');
  }
  for ($i = 0; $i < $form_state
    ->getValue('num_new'); $i++) {
    $form['perms']['local']['new']['status'] = [
      '#type' => 'checkbox',
      '#default_value' => '',
    ];
    $form['perms']['local']['new']['name'] = [
      '#type' => 'textfield',
      '#default_value' => '',
      '#size' => 30,
    ];
    $form['perms']['local']['new']['path'] = [
      '#type' => 'textarea',
      '#default_value' => '',
      '#rows' => 2,
      '#size' => 50,
    ];
  }
  $form['perms']['add']['status'] = [
    '#name' => 'status',
    '#id' => 'edit-local-status',
    '#type' => 'submit',
    '#value' => $this
      ->t('Add permission'),
    '#submit' => [
      '::configPermsAdminFormAddSubmit',
    ],
    '#ajax' => [
      'callback' => '::configPermsAdminFormAddCallback',
      'wrapper' => 'config_perms-wrapper',
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}