Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / EventSubscriber / SubmissionEventSubscriber.php
Size: Mime:
<?php


namespace Drupal\custom_forms\EventSubscriber;


use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\custom_forms\CustomFormsSubmissionHandlerManager;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SubmissionEventSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The logger channel factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * @var \Drupal\custom_forms\CustomFormsSubmissionHandlerManager
   *   The plugin manager for submission handler plugins.
   */
  protected $pluginManager;

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   *   The storage handler for submission handler entities.
   */
  protected $handlerStorage;

  /**
   * @var \Drupal\Core\Queue\QueueInterface
   *   The queue for submission handlers.
   */
  protected $queue;

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events = [
      'custom_form.finalize.post_transition'  => ['executeHandlers', -100],
    ];
    return $events;
  }

  /**
   * @inheritDoc
   */
  public function __construct(
    CustomFormsSubmissionHandlerManager $pluginManager,
    EntityTypeManagerInterface $entityTypeManager,
    LoggerChannelFactoryInterface $logger_factory,
    QueueFactory $queue_factory
  ) {
    $this->logger           = $logger_factory->get('Custom Forms');
    $this->pluginManager    = $pluginManager;
    $this->handlerStorage   = $entityTypeManager->getStorage('custom_form_submission_handler');
    $this->queue            = $queue_factory->get('custom_forms_submission_handlers');
  }

  public function executeHandlers(WorkflowTransitionEvent $event) {
    /** @var \Drupal\custom_forms\Entity\CustomFormSubmissionInterface $submission */
    $submission   = $event->getEntity();
    /** @var \Drupal\custom_forms\CustomFormInterface $custom_form */
    $custom_form  = $submission->getForm();

    /** @var \Drupal\custom_forms\Entity\CustomFormSubmissionHandlerInterface[] $handlers */
    $handlers = $this->handlerStorage->loadByProperties([
      'form' => $custom_form->id()
    ]);

    if (!empty($handlers)) {
      foreach ($handlers as $handler) {
        $data = [
          'handler_id' => $handler->id(),
          'submission_id' => $submission->id(),
        ];
        $this->queue->createItem($data);
      }
    }
  }

}