You are here

function workflow_entity_delete in Workflow 8

Implements hook_entity_delete().

Delete the corresponding workflow table records.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: An Entity.

File

./workflow.module, line 219
Support workflows made up of arbitrary states.

Code

function workflow_entity_delete(EntityInterface $entity) {

  // @todo Test with multiple workflows.
  if (get_class($entity) == 'Drupal\\field\\Entity\\FieldConfig' || get_class($entity) == 'Drupal\\field\\Entity\\FieldStorageConfig') {

    // A Workflow Field is removed from an entity.
    $field_config = $entity;

    /** @var \Drupal\Core\Entity\ContentEntityBase $field_config */
    $entity_type = $field_config
      ->get('entity_type');
    $field_name = $field_config
      ->get('field_name');

    /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
    foreach (WorkflowScheduledTransition::loadMultipleByProperties($entity_type, [], [], $field_name) as $transition) {
      $transition
        ->delete();
    }
    foreach (WorkflowTransition::loadMultipleByProperties($entity_type, [], [], $field_name) as $transition) {
      $transition
        ->delete();
    }
  }
  elseif (!WorkflowManager::isWorkflowEntityType($entity
    ->getEntityTypeId())) {

    // A 'normal' entity is deleted.
    foreach ($fields = _workflow_info_fields($entity) as $field_id => $field_storage) {
      $entity_id = $entity
        ->id();
      $entity_type = $field_storage
        ->getTargetEntityTypeId();
      $field_name = $field_storage
        ->getName();

      /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
      foreach (WorkflowScheduledTransition::loadMultipleByProperties($entity_type, [
        $entity_id,
      ], [], $field_name) as $transition) {
        $transition
          ->delete();
      }
      foreach (WorkflowTransition::loadMultipleByProperties($entity_type, [
        $entity_id,
      ], [], $field_name) as $transition) {
        $transition
          ->delete();
      }
    }
  }
}