You are here

public function EntitySubqueuePreSave::onPreEntitySave in Acquia Content Hub 8.2

Deletes entity subqueue if it's already created with diff UUID.

Parameters

\Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event: The pre entity save event.

Throws

\Drupal\Core\Entity\EntityStorageException

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/PreEntitySave/EntitySubqueuePreSave.php, line 67

Class

EntitySubqueuePreSave
Handles entity subqueue import failure.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave

Code

public function onPreEntitySave(PreEntitySaveEvent $event) {
  if ($event
    ->getEntity()
    ->getEntityTypeId() !== self::ENTITY_SUBQUEUE) {
    return;
  }
  $uuid = $event
    ->getEntity()
    ->uuid();
  $subqueue_id = $event
    ->getEntity()
    ->id();
  $query = $this->database
    ->select(self::ENTITY_SUBQUEUE, 'es');
  $query
    ->fields('es', [
    'uuid',
  ]);
  $query
    ->condition('name', $subqueue_id);
  $query
    ->condition('queue', $subqueue_id);
  $result = $query
    ->execute()
    ->fetchField();
  if ($result && $uuid !== $result) {
    $entity_subqueue = $this->entityTypeManager
      ->getStorage('entity_subqueue')
      ->load($subqueue_id);
    $entity_subqueue
      ->delete();
    $event
      ->stopPropagation();
  }
}