You are here

class RequestTranslationSubscriber in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 src/EventSubscriber/RequestTranslationSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestTranslationSubscriber
  2. 8.2 src/EventSubscriber/RequestTranslationSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestTranslationSubscriber
  3. 8.3 src/EventSubscriber/RequestTranslationSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestTranslationSubscriber

Hierarchy

  • class \Drupal\tmgmt_smartling\EventSubscriber\RequestTranslationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RequestTranslationSubscriber

1 string reference to 'RequestTranslationSubscriber'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses RequestTranslationSubscriber
tmgmt_smartling.subscriber.request_translation in ./tmgmt_smartling.services.yml
\Drupal\tmgmt_smartling\EventSubscriber\RequestTranslationSubscriber

File

src/EventSubscriber/RequestTranslationSubscriber.php, line 12

Namespace

Drupal\tmgmt_smartling\EventSubscriber
View source
class RequestTranslationSubscriber implements EventSubscriberInterface {
  const WAIT_BEFORE_CONTEXT_UPLOAD = 600;
  protected $contextUploadQueue;
  public function __construct(QueueFactory $queue, TranslationJobToUrl $url_converter) {
    $this->contextUploadQueue = $queue
      ->get('smartling_context_upload', TRUE);
    $this->urlConverter = $url_converter;
  }

  /**
   * Code that should be triggered on event specified
   */
  public function onUploadRequest(RequestTranslationEvent $event) {

    /** @var JobInterface $job */
    $job = $event
      ->getJob();
    if (!$job
      ->getTranslator()
      ->getPlugin() instanceof SmartlingTranslator) {
      return;
    }
    $job_items = $job
      ->getItems();
    if (empty($job_items)) {
      return;
    }
    $filename = $job
      ->getTranslatorPlugin()
      ->getFileName($job);
    foreach ($job_items as $item) {
      $url = $this->urlConverter
        ->convert($item);
      $cloned_item = clone $item;
      \Drupal::moduleHandler()
        ->alter('tmgmt_smartling_context_url', $url, $cloned_item);
      if (!empty($url)) {
        $this->contextUploadQueue
          ->createItem([
          'url' => $url,
          'filename' => $filename,
          'job_id' => $job
            ->id(),
          'upload_date' => time() + self::WAIT_BEFORE_CONTEXT_UPLOAD,
        ]);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // For this example I am using KernelEvents constants (see below a full list).
    $events = [];
    $events[RequestTranslationEvent::REQUEST_TRANSLATION_EVENT][] = [
      'onUploadRequest',
    ];
    return $events;
  }

}

Members