You are here

function permissions_lock_form_alter in Permissions Lock 8

Same name and namespace in other branches
  1. 7 permissions_lock.module \permissions_lock_form_alter()

Implements hook_form_alter().

File

./permissions_lock.module, line 28
Lock permissions on the permissions administration pages for certain roles

Code

function permissions_lock_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'user_admin_permissions' || $form_id == 'user_permissions_profile_permissions_form') {

    // Get all available permissions in the form.
    $config = \Drupal::config('permissions_lock.settings');
    $default_roles = $config
      ->get('permissions_lock_locked_roles');
    $default_perms = $config
      ->get('permissions_lock_locked_perm');
    if (!\Drupal::currentUser()
      ->hasPermission('manage permissions unrestricted')) {

      // Get all locked roles from permission lock form.
      $locked_roles = array();
      foreach ($default_roles as $key => $value) {
        if ($value) {
          $locked_roles[$key] = $value;
        }
      }

      // Get all locked permissions from permission lock form.
      $locked_permissions = array();
      foreach ($default_perms as $key => $value) {
        if ($value) {
          $locked_permissions[$key] = $value;
        }
      }

      // Hide all the locked roles.
      foreach ($locked_roles as $key => $locked_rid) {
        foreach ($default_perms as $key1 => $value1) {
          unset($form['permissions'][$key1][$key]);
        }
        foreach ($form['permissions']['#header'] as $key2 => $value2) {
          if (is_array($value2)) {
            if ($value2['data'] == user_roles()[$key]
              ->label()) {
              unset($form['permissions']['#header'][$key2]);
            }
          }
        }
      }

      // Hide all the locked permissions.
      foreach ($locked_permissions as $key => $value) {
        unset($form['permissions'][$key]);
      }
    }
  }
}