You are here

public function DuplicateRoleForm::buildForm in Duplicate role 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 FormInterface::buildForm

File

src/Form/DuplicateRoleForm.php, line 69

Class

DuplicateRoleForm
Provides a form for adding a new role.

Namespace

Drupal\duplicate_role\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $note = NULL) {
  $u_roles = user_role_names();
  asort($u_roles);
  $options = [];
  $options[''] = $this
    ->t('-select-');
  foreach ($u_roles as $key => $value) {
    $options[$key] = $value;
  }

  // Try to get a base role from route parameter.
  $base_role = $this->routeMatch
    ->getParameter('role');
  $form['base_role'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Choose role to duplicate'),
    '#description' => $this
      ->t('Select role to duplicate'),
    '#options' => $options,
    '#required' => TRUE,
    '#access' => !isset($options[$base_role]),
  ];
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('New role'),
    '#required' => TRUE,
    '#size' => 40,
    '#maxlength' => 40,
    '#description' => $this
      ->t('The name for the duplicated role. Example: "Moderator", "Editorial board", "Site architect".'),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => '',
    '#required' => TRUE,
    '#size' => 30,
    '#maxlength' => 64,
    '#machine_name' => [
      'exists' => [
        Role::class,
        'load',
      ],
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Duplicate'),
  ];
  return $form;
}