You are here

protected function CustomAccess::addPermissionsGrid in Field Permissions 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/FieldPermissionType/CustomAccess.php \Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess::addPermissionsGrid()

Attach a permissions grid to the field edit form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

\Drupal\user\RoleStorageInterface $role_storage: The user role storage.

1 call to CustomAccess::addPermissionsGrid()
CustomAccess::buildAdminForm in src/Plugin/FieldPermissionType/CustomAccess.php
Build or alter the field admin form.

File

src/Plugin/FieldPermissionType/CustomAccess.php, line 125

Class

CustomAccess
Defines custom access for fields.

Namespace

Drupal\field_permissions\Plugin\FieldPermissionType

Code

protected function addPermissionsGrid(array &$form, FormStateInterface $form_state, RoleStorageInterface $role_storage) {

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = $role_storage
    ->loadMultiple();
  $permissions = $this
    ->getPermissions();
  $options = array_keys($permissions);

  // The permissions table.
  $form['permissions'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Permission'),
    ],
    '#id' => 'permissions',
    '#attributes' => [
      'class' => [
        'permissions',
        'js-permissions',
      ],
    ],
    '#sticky' => TRUE,
  ];
  foreach ($roles as $role) {
    $form['permissions']['#header'][] = [
      'data' => $role
        ->label(),
      'class' => [
        'checkbox',
      ],
    ];
  }

  // @todo Remove call to global service.
  $test = \Drupal::service('field_permissions.permissions_service')
    ->getPermissionsByRole();
  foreach ($permissions as $provider => $permission) {
    $form['permissions'][$provider]['description'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="permission"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em> {% endif %}{{ description }}</div>{% endif %}</div>',
      '#context' => [
        'title' => $permission["title"],
      ],
    ];
    $options[$provider] = '';
    foreach ($roles as $name => $role) {
      $form['permissions'][$provider][$name] = [
        '#title' => $name . ': ' . $permission["title"],
        '#title_display' => 'invisible',
        '#type' => 'checkbox',
        '#attributes' => [
          'class' => [
            'rid-' . $name,
            'js-rid-' . $name,
          ],
        ],
        '#wrapper_attributes' => [
          'class' => [
            'checkbox',
          ],
        ],
      ];
      if (!empty($test[$name]) && in_array($provider, $test[$name])) {
        $form['permissions'][$provider][$name]['#default_value'] = in_array($provider, $test[$name]);
      }
      if ($role
        ->isAdmin()) {
        $form['permissions'][$provider][$name]['#disabled'] = TRUE;
        $form['permissions'][$provider][$name]['#default_value'] = TRUE;
      }
    }
  }

  // Attach the Drupal user permissions library.
  $form['#attached']['library'][] = 'user/drupal.user.permissions';
}