You are here

public function RoleAccessControlAdminForm::buildForm in Role Access Control 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/RoleAccessControlAdminForm.php, line 23

Class

RoleAccessControlAdminForm
Contribute form.

Namespace

Drupal\rac\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $roles = user_roles();
  $config = $this
    ->config("rac.settings");
  $form['general'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('General Settings'),
  ];
  $form['general']['update_unpublished'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Apply update grants to unpublished content.'),
    '#default_value' => $config
      ->get("update_unpublished"),
  ];
  $form["table_title"] = [
    "#type" => "item",
    '#title' => $this
      ->t('Update Grants'),
    '#description' => $this
      ->t('For each row, configure which roles (columns) for which editing is approved.'),
  ];
  $form['role_access'] = [
    '#type' => 'table',
    '#header' => [
      '',
    ],
    '#id' => 'role-access-all',
    '#attributes' => [
      'class' => [
        'role-access-all',
        'js-role-access-all',
      ],
    ],
    '#sticky' => TRUE,
  ];
  foreach ($roles as $role) {
    $form['role_access']['#header'][] = [
      'data' => $role
        ->label(),
      'class' => [
        'checkbox',
      ],
    ];
  }
  foreach ($roles as $rid => $role) {

    // Fill in default values for the permission.
    $form['role_access'][$rid]['description'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="permission"><span class="title">{{ title }}</span></div>',
      '#context' => [
        'title' => $role
          ->label(),
      ],
    ];
    foreach ($roles as $srid => $srole) {
      $permission = "RAC_update_" . $srole
        ->id();
      $form['role_access'][$rid][$srid] = [
        '#title' => $role
          ->label() . ' can edit for ' . $srole
          ->label(),
        '#title_display' => 'invisible',
        '#wrapper_attributes' => [
          'class' => [
            'checkbox',
          ],
        ],
        '#type' => 'checkbox',
        '#default_value' => 0,
        '#attributes' => [
          'class' => [
            'rid-' . $srid,
            'js-rid-' . $srid,
          ],
        ],
        '#parents' => [
          $rid,
          $srid,
        ],
      ];
      if ($role
        ->isAdmin()) {
        $form['role_access'][$rid][$srid]["#disabled"] = TRUE;
      }
      if ($role
        ->hasPermission($permission)) {
        $form['role_access'][$rid][$srid]['#default_value'] = 1;
      }
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save permissions'),
    '#button_type' => 'primary',
  ];
  return $form;
}