You are here

public static function Webform::preDelete in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Entity/Webform.php \Drupal\webform\Entity\Webform::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 ConfigEntityBase::preDelete

File

src/Entity/Webform.php, line 2201

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {

  /** @var \Drupal\webform\WebformInterface[] $entities */
  parent::preDelete($storage, $entities);

  /** @var \Drupal\user\UserDataInterface $user_data */
  $user_data = \Drupal::service('user.data');

  // Delete all paths, states, and user data associated with this webform.
  foreach ($entities as $entity) {

    // Delete all paths.
    $entity
      ->deletePaths();

    // Delete the state.
    // @see \Drupal\webform\Entity\Webform::getState
    \Drupal::state()
      ->delete('webform.webform.' . $entity
      ->id());

    // Delete user data.
    // @see \Drupal\webform\Entity\Webform::getUserData
    $user_data
      ->delete('webform', NULL, $entity
      ->id());
  }

  // Delete all submission associated with this webform.
  $submission_ids = \Drupal::entityQuery('webform_submission')
    ->accessCheck(FALSE)
    ->condition('webform_id', array_keys($entities), 'IN')
    ->sort('sid')
    ->execute();
  $submission_storage = \Drupal::entityTypeManager()
    ->getStorage('webform_submission');
  $submissions = $submission_storage
    ->loadMultiple($submission_ids);
  $submission_storage
    ->delete($submissions);
}