You are here

public function Role::onDependencyRemoval in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role::onDependencyRemoval()

Informs the entity that entities it depends on will be deleted.

This method allows configuration entities to remove dependencies instead of being deleted themselves. Configuration entities can use this method to avoid being unnecessarily deleted during an extension uninstallation. For example, entity displays remove references to widgets and formatters if the plugin that supplies them depends on a module that is being uninstalled.

If this method returns TRUE then the entity needs to be re-saved by the caller for the changes to take effect. Implementations should not save the entity.

Parameters

array $dependencies: An array of dependencies that will be deleted keyed by dependency type. Dependency types are, for example, entity, module and theme.

Return value

bool TRUE if the entity has been changed as a result, FALSE if not.

Overrides ConfigEntityBase::onDependencyRemoval

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

\Drupal\Core\Config\ConfigEntityBase::preDelete()

\Drupal\Core\Config\ConfigManager::uninstall()

\Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval()

File

core/modules/user/src/Entity/Role.php, line 223

Class

Role
Defines the user role entity class.

Namespace

Drupal\user\Entity

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = parent::onDependencyRemoval($dependencies);

  // Load all permission definitions.
  $permission_definitions = \Drupal::service('user.permissions')
    ->getPermissions();

  // Convert config and content entity dependencies to a list of names to make
  // it easier to check.
  foreach ([
    'content',
    'config',
  ] as $type) {
    $dependencies[$type] = array_keys($dependencies[$type]);
  }

  // Remove any permissions from the role that are dependent on anything being
  // deleted or uninstalled.
  foreach ($this->permissions as $key => $permission) {
    if (!isset($permission_definitions[$permission])) {

      // If the permission is not defined then there's nothing we can do.
      continue;
    }
    if (in_array($permission_definitions[$permission]['provider'], $dependencies['module'], TRUE)) {
      unset($this->permissions[$key]);
      $changed = TRUE;

      // Process the next permission.
      continue;
    }
    if (isset($permission_definitions[$permission]['dependencies'])) {
      foreach ($permission_definitions[$permission]['dependencies'] as $type => $list) {
        if (array_intersect($list, $dependencies[$type])) {
          unset($this->permissions[$key]);
          $changed = TRUE;

          // Process the next permission.
          continue 2;
        }
      }
    }
  }
  return $changed;
}