You are here

public function WorkbenchAccessByUserForm::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/WorkbenchAccessByUserForm.php, line 74

Class

WorkbenchAccessByUserForm
Configure Workbench Access per user.

Namespace

Drupal\workbench_access\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, AccessSchemeInterface $access_scheme = NULL, $id = NULL) {
  $this->scheme = $access_scheme;
  $existing_editors = $this->userSectionStorage
    ->getEditors($access_scheme, $id);
  $potential_editors = $this->userSectionStorage
    ->getPotentialEditors($id);
  $form['existing_editors'] = [
    '#type' => 'value',
    '#value' => $existing_editors,
  ];
  $form['section_id'] = [
    '#type' => 'value',
    '#value' => $id,
  ];
  $form['add'] = [
    '#type' => 'container',
  ];
  if ($potential_editors) {
    $toggle = '<br>' . $this
      ->t('<a class="switch" href="#">Switch between textarea/autocomplete</a>');
    $form['add']['editors_add'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'user',
      '#selection_handler' => 'workbench_access:user:' . $access_scheme
        ->id(),
      '#selection_settings' => [
        'include_anonymous' => FALSE,
        'match_operator' => 'STARTS_WITH',
        'filter' => [
          'section_id' => $id,
        ],
      ],
      '#title' => $this
        ->t('Add editors to the %label section.', [
        '%label' => $access_scheme
          ->label(),
      ]),
      '#description' => $this
        ->t('Search editors to add to this section, separate with comma to add multiple editors.<br>Only users in roles with permission to be assigned can be referenced.') . $toggle,
      '#tags' => TRUE,
    ];

    // The authenticated user role is not stored in the database, so we cannot
    // query for it. If 'authenticated user' is present, do not filter on
    // roles at all.
    $potential_editors_roles = $this->userSectionStorage
      ->getPotentialEditorsRoles($id);
    if (!isset($potential_editors_roles[AccountInterface::AUTHENTICATED_ROLE])) {

      // Add the role filter, which uses the role id stored as array_keys().
      $form['add']['editors_add']['#selection_settings']['filter'] = [
        'role' => array_keys($potential_editors_roles),
        'section_id' => $id,
      ];
    }
    $form['add']['editors_add_mass'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Add editors to the %label section.', [
        '%label' => $access_scheme
          ->label(),
      ]),
      '#description' => $this
        ->t('Add a list of user ids or usernames separated with comma or new line. Invalid or existing users will be ignored.') . $toggle,
    ];
    $form['add']['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#name' => 'add',
        '#value' => $this
          ->t('Add'),
      ],
    ];
  }
  else {
    $form['add']['message'] = [
      '#type' => 'markup',
      '#markup' => '<p>' . $this
        ->t('There are no additional users that can be added to the %label section', [
        '%label' => $access_scheme
          ->label(),
      ]) . '</p>',
    ];
  }
  $form['remove'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Existing editors in the %label section.', [
      '%label' => $access_scheme
        ->label(),
    ]),
    '#description' => $this
      ->t('<p>Current editors list. Use the checkboxes to remove editors from this section.</p>'),
  ];

  // Prepare editors list for tableselect.
  $editors_data = [];

  // Do we neeed to paginate?
  $total = count($existing_editors);
  $limit = 50;
  $existing = array_chunk($existing_editors, $limit, TRUE);
  $pages = count($existing);

  // pager_default_initialize() is deprecated in 8.8.
  if (\Drupal::hasService('pager.manager')) {
    $pager_manager = \Drupal::service('pager.manager');
    $pager = $pager_manager
      ->createPager($total, $limit);
    $page = $pager
      ->getCurrentPage();
  }
  else {
    $page = pager_default_initialize($total, $limit);
  }
  $start = $page * $limit;
  if ($pages > 1) {
    $existing_editors = $existing[$page];
    $form['remove']['count'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('<p>Page @x of @pages. Showing editors @start - @end of @count total.</p>', [
        '@x' => $page + 1,
        '@pages' => $pages,
        '@start' => $start + 1,
        '@end' => $start + count($existing_editors),
        '@count' => $total,
      ]),
    ];
    $form['remove']['pagination'] = [
      '#type' => 'pager',
      '#weight' => 2,
    ];
  }
  if ($existing_editors) {
    foreach ($existing_editors as $uid => $username) {
      $editors_data[$uid] = [
        $username,
      ];
    }
    asort($editors_data);
  }
  $form['remove']['editors_remove'] = [
    '#type' => 'tableselect',
    '#header' => [
      $this
        ->t('Username'),
    ],
    '#options' => $editors_data,
    '#empty' => $this
      ->t('There are no editors assigned to the %label section.', [
      '%label' => $access_scheme
        ->label(),
    ]),
  ];
  if ($existing_editors) {
    $form['remove']['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#name' => 'remove',
        '#value' => $this
          ->t('Remove'),
      ],
    ];
  }
  $form['#attached']['library'][] = 'workbench_access/admin';
  return $form;
}