You are here

class PfmUserPermissionsForm in Permissions filtered by modules 8

Provides the user permissions administration PFM module form.

Hierarchy

Expanded class hierarchy of PfmUserPermissionsForm

1 string reference to 'PfmUserPermissionsForm'
pfm.routing.yml in ./pfm.routing.yml
pfm.routing.yml

File

src/Form/PfmUserPermissionsForm.php, line 11

Namespace

Drupal\pfm\Form
View source
class PfmUserPermissionsForm extends UserPermissionsForm {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'pfm_user_admin_permissions';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $input = $form_state
      ->getUserInput();
    $form['#prefix'] = '<div id="pfm-user-admin-permissions-wrapper">';
    $form['#suffix'] = '</div>';
    $form['permissions_filter'] = [
      '#type' => 'fieldset',
      '#title' => 'Permissions Filters',
      '#weight' => -10,
      '#attributes' => [
        'class' => [
          'form--inline',
          'clearfix',
        ],
      ],
    ];
    $role_names = [];
    $role_permissions = [];
    $admin_roles = [];
    foreach ($this
      ->getRoles() as $role_name => $role) {

      // Retrieve role names for columns.
      $role_names[$role_name] = $role
        ->label();

      // Fetch permissions for the roles.
      $role_permissions[$role_name] = $role
        ->getPermissions();
      $admin_roles[$role_name] = $role
        ->isAdmin();
    }
    $roles_options = [
      'all' => 'All Roles',
    ] + $role_names;
    $form['permissions_filter']['roles'] = [
      '#type' => 'select',
      '#options' => $roles_options,
      '#title' => $this
        ->t('Select Role'),
      '#multiple' => TRUE,
      '#default_value' => 'all',
      '#weight' => -8,
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxRefresh',
        ],
        'wrapper' => 'pfm-user-admin-permissions-wrapper',
        'progress' => [
          'type' => 'fullscreen',
        ],
      ],
    ];

    // Store $role_names for use when saving the data.
    $form['role_names'] = [
      '#type' => 'value',
      '#value' => $role_names,
    ];

    // Render role/permission overview:
    $form['permissions'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Permission'),
      ],
      '#id' => 'permissions',
      '#attributes' => [
        'class' => [
          'permissions',
          'js-permissions',
        ],
      ],
      '#sticky' => TRUE,
      '#empty' => $this
        ->t('Please choose at least one module.'),
    ];
    $selected_roles = isset($input['roles']) ? $input['roles'] : [];
    $selected_roles = array_intersect($selected_roles, array_keys($roles_options));
    foreach ($role_names as $role_id => $name) {
      if (in_array('all', $selected_roles) || in_array($role_id, $selected_roles)) {
        $form['permissions']['#header'][] = [
          'data' => $name,
          'class' => [
            'checkbox',
          ],
        ];
      }
    }
    $permissions = $this->permissionHandler
      ->getPermissions();
    $providers = $permissions_by_provider = [];
    foreach ($permissions as $permission_name => $permission) {
      $permissions_by_provider[$permission['provider']][$permission_name] = $permission;
      $providers[] = $permission['provider'];
    }
    $providers = array_unique($providers);
    $modules_list = array_keys($this->moduleHandler
      ->getModuleList());
    $modules_list = array_intersect($modules_list, $providers);
    $modules_options = array_map(function ($module_name) {
      return $this->moduleHandler
        ->getName($module_name);
    }, array_combine($modules_list, $modules_list));
    natcasesort($modules_options);
    $selected_modules = isset($input['modules']) ? $input['modules'] : [];
    $selected_modules = array_intersect($selected_modules, $modules_list);
    $form['permissions_filter']['modules'] = [
      '#type' => 'select',
      '#options' => $modules_options,
      '#title' => $this
        ->t('Select Modules'),
      '#multiple' => TRUE,
      '#weight' => -9,
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxRefresh',
        ],
        'wrapper' => 'pfm-user-admin-permissions-wrapper',
        'progress' => [
          'type' => 'fullscreen',
        ],
      ],
    ];
    if (empty($selected_modules)) {
      return $form;
    }

    // Move the access content permission to the Node module if it is installed.
    if ($this->moduleHandler
      ->moduleExists('node')) {

      // Insert 'access content' before the 'view own unpublished content' key
      // in order to maintain the UI even though the permission is provided by
      // the system module.
      $keys = array_keys($permissions_by_provider['node']);
      $offset = (int) array_search('view own unpublished content', $keys);
      $permissions_by_provider['node'] = array_merge(array_slice($permissions_by_provider['node'], 0, $offset), [
        'access content' => $permissions_by_provider['system']['access content'],
      ], array_slice($permissions_by_provider['node'], $offset));
      unset($permissions_by_provider['system']['access content']);
    }
    $table_build_info = [
      $permissions_by_provider,
      $role_names,
      $role_permissions,
      $admin_roles,
      $selected_modules,
      $selected_roles,
    ];

    // Add method for possible overrides if needed.
    $this
      ->fillPermissionTable($form, $form_state, $table_build_info);
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save permissions'),
      '#button_type' => 'primary',
    ];
    $form['#attached']['library'][] = 'user/drupal.user.permissions';

    // Add extra functionality if permissions_dragcheck module exist.
    if ($this->moduleHandler
      ->moduleExists('permissions_dragcheck')) {

      // Add the library:
      $form['#attached']['library'][] = 'permissions_dragcheck/drag-check-js';

      // Init:
      $form['#attached']['library'][] = 'permissions_dragcheck/permissions-drag-check';
    }
    return $form;
  }

  /**
   * Helper method for filling Permission Table.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $table_build_info
   *   The current table build information.
   */
  protected function fillPermissionTable(array &$form, FormStateInterface &$form_state, array $table_build_info) {
    list($permissions_by_provider, $role_names, $role_permissions, $admin_roles, $selected_modules, $selected_roles, ) = $table_build_info;
    foreach ($permissions_by_provider as $provider => $permissions) {

      // Module name.
      if (!in_array($provider, $selected_modules)) {
        continue;
      }
      $form['permissions'][$provider] = [
        [
          '#wrapper_attributes' => [
            'colspan' => count($role_names) + 1,
            'class' => [
              'module',
            ],
            'id' => 'module-' . $provider,
          ],
          '#markup' => $this->moduleHandler
            ->getName($provider),
        ],
      ];
      foreach ($permissions as $perm => $perm_item) {

        // Fill in default values for the permission.
        $perm_item += [
          'description' => '',
          'restrict access' => FALSE,
          'warning' => !empty($perm_item['restrict access']) ? $this
            ->t('Warning: Give to trusted roles only; this permission has security implications.') : '',
        ];
        $form['permissions'][$perm]['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' => $perm_item['title'],
          ],
        ];
        $form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
        $form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
        foreach ($role_names as $rid => $name) {
          if (in_array('all', $selected_roles) || in_array($rid, $selected_roles)) {
            $checked = in_array($perm, $role_permissions[$rid]) ? TRUE : FALSE;
            $form['permissions'][$perm][$rid] = [
              '#title' => $name . ': ' . $perm_item['title'],
              '#title_display' => 'invisible',
              '#wrapper_attributes' => [
                'class' => [
                  'checkbox',
                ],
              ],
              '#type' => 'checkbox',
              '#default_value' => $checked,
              '#attributes' => [
                'class' => [
                  'rid-' . $rid,
                  'js-rid-' . $rid,
                ],
                'checked' => $checked,
              ],
              '#parents' => [
                $rid,
                $perm,
              ],
            ];

            // Show a column of disabled but checked checkboxes.
            if ($admin_roles[$rid]) {
              $form['permissions'][$perm][$rid]['#disabled'] = TRUE;
              $form['permissions'][$perm][$rid]['#default_value'] = TRUE;
            }
          }
        }
      }
    }
  }

  /**
   * AJAX refresh callback for the form.
   */
  public function ajaxRefresh(&$form, FormStateInterface $form_state) {
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PfmUserPermissionsForm::ajaxRefresh public function AJAX refresh callback for the form.
PfmUserPermissionsForm::buildForm public function Form constructor. Overrides UserPermissionsForm::buildForm
PfmUserPermissionsForm::fillPermissionTable protected function Helper method for filling Permission Table.
PfmUserPermissionsForm::getFormId public function Returns a unique string identifying the form. Overrides UserPermissionsForm::getFormId
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
UserPermissionsForm::$moduleHandler protected property The module handler.
UserPermissionsForm::$permissionHandler protected property The permission handler.
UserPermissionsForm::$roleStorage protected property The role storage.
UserPermissionsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
UserPermissionsForm::getRoles protected function Gets the roles to display in this form. 1
UserPermissionsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
UserPermissionsForm::__construct public function Constructs a new UserPermissionsForm.