You are here

public function CustomAccess::submitAdminForm 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::submitAdminForm()

Allows the plugin to react to the field settings form submission.

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.

Overrides AdminFormSettingsInterface::submitAdminForm

File

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

Class

CustomAccess
Defines custom access for fields.

Namespace

Drupal\field_permissions\Plugin\FieldPermissionType

Code

public function submitAdminForm(array &$form, FormStateInterface $form_state, RoleStorageInterface $role_storage) {
  $custom_permissions = $form_state
    ->getValue('permissions');

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = [];
  foreach ($custom_permissions as $permission_name => $field_perm) {
    foreach ($field_perm as $role_name => $role_permission) {
      $roles[$role_name] = $role_storage
        ->load($role_name);

      // If using this plugin, set permissions to the value submitted in the
      // form. Otherwise remove all permissions as they will no longer exist.
      $role_permission = $form_state
        ->getValue('type') === $this
        ->getPluginId() ? $role_permission : FALSE;
      if ($role_permission) {
        $roles[$role_name]
          ->grantPermission($permission_name);
      }
      else {
        $roles[$role_name]
          ->revokePermission($permission_name);
      }
    }
  }

  // Save all roles.
  foreach ($roles as $role) {
    $role
      ->trustData()
      ->save();
  }
}