You are here

class EntitySubqueuePreSave in Acquia Content Hub 8.2

Handles entity subqueue import failure.

@package Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave

Hierarchy

  • class \Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave\EntitySubqueuePreSave implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EntitySubqueuePreSave

1 file declares its use of EntitySubqueuePreSave
EntitySubqueuePreSaveTest.php in tests/src/Kernel/EventSubscriber/PreEntitySave/EntitySubqueuePreSaveTest.php
1 string reference to 'EntitySubqueuePreSave'
acquia_contenthub_subscriber.services.yml in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
1 service uses EntitySubqueuePreSave
acquia_contenthub.entity_subqueue.pre_entity_save in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave\EntitySubqueuePreSave

File

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

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave
View source
class EntitySubqueuePreSave implements EventSubscriberInterface {

  /**
   * The entity subqueue.
   *
   * @var string
   */
  const ENTITY_SUBQUEUE = 'entity_subqueue';

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * EntitySubqueuePreSave constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Database connection.
   */
  public function __construct(Connection $database) {

    // Using \Drupal::entityTypeManager() do to caching of the instance in
    // some services. Looks like a core bug.
    $this->entityTypeManager = \Drupal::entityTypeManager();
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::PRE_ENTITY_SAVE] = [
      'onPreEntitySave',
      80,
    ];
    return $events;
  }

  /**
   * Deletes entity subqueue if it's already created with diff UUID.
   *
   * @param \Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event
   *   The pre entity save event.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  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();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntitySubqueuePreSave::$database protected property The database connection.
EntitySubqueuePreSave::$entityTypeManager protected property The module handler.
EntitySubqueuePreSave::ENTITY_SUBQUEUE constant The entity subqueue.
EntitySubqueuePreSave::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntitySubqueuePreSave::onPreEntitySave public function Deletes entity subqueue if it's already created with diff UUID.
EntitySubqueuePreSave::__construct public function EntitySubqueuePreSave constructor.