You are here

public function AssignUserForm::buildForm in Workbench Access 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/AssignUserForm.php, line 127

Class

AssignUserForm
Builds the workbench_access set switch form.

Namespace

Drupal\workbench_access\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
  $account = $this
    ->currentUser();
  $this->user = $user;
  $form_enabled = FALSE;
  $active_schemes = [];

  // Load all schemes.
  $schemes = $this->schemeStorage
    ->loadMultiple();
  foreach ($schemes as $scheme) {
    $user_sections = $this->userSectionStorage
      ->getUserSections($scheme, $user, FALSE);
    $options = $this
      ->getFormOptions($scheme);
    $role_sections = $this->roleSectionStorage
      ->getRoleSections($scheme, $user);
    $list = array_flip($role_sections);
    foreach ($options as $value => $label) {
      if (isset($list[$value])) {
        $options[$value] = '<strong>' . $label . ' * </strong>';
      }
    }
    if (!empty($options)) {
      $form[$scheme
        ->id()] = [
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#title' => $scheme
          ->getPluralLabel(),
      ];
      $form[$scheme
        ->id()]['active_' . $scheme
        ->id()] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Assigned sections'),
        '#options' => $options,
        '#default_value' => $user_sections,
        '#description' => $this
          ->t('Sections assigned by role are <strong>emphasized</strong> and marked with an * but not selected unless they are also assigned directly to the user. They need not be selected. Access granted by role cannot be revoked from this form.'),
      ];
      $form[$scheme
        ->id()]['scheme_' . $scheme
        ->id()] = [
        '#type' => 'value',
        '#value' => $scheme,
      ];
      $form_enabled = TRUE;
      $active_schemes[] = $scheme
        ->id();
    }
  }
  if ($form_enabled) {
    $form['schemes'] = [
      '#type' => 'value',
      '#value' => $active_schemes,
    ];
    $form['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#name' => 'save',
        '#value' => $this
          ->t('Save'),
      ],
    ];
  }
  else {
    $form['help'] = [
      '#markup' => $this
        ->t('You do not have permission to manage any assignments.'),
    ];
  }
  return $form;
}