You are here

public static function Workspace::preDelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/Entity/Workspace.php \Drupal\workspaces\Entity\Workspace::preDelete()

Acts on entities before they are deleted and before hooks are invoked.

Used before the entities are deleted and before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides EntityBase::preDelete

File

core/modules/workspaces/src/Entity/Workspace.php, line 157

Class

Workspace
The workspace entity class.

Namespace

Drupal\workspaces\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {
  parent::preDelete($storage, $entities);
  $workspace_tree = \Drupal::service('workspaces.repository')
    ->loadTree();

  // Ensure that workspaces that have descendants can not be deleted.
  foreach ($entities as $entity) {
    if (!empty($workspace_tree[$entity
      ->id()]['descendants'])) {
      throw new \InvalidArgumentException("The {$entity->label()} workspace can not be deleted because it has child workspaces.");
    }
  }
}