You are here

DownloadTmgmtActionApproveForm.php in TMGMT Extension Suite 8

File

src/Form/DownloadTmgmtActionApproveForm.php
View source
<?php

namespace Drupal\tmgmt_extension_suit\Form;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;

/**
 * Provides a confirmation form for sending multiple content entities.
 */
class DownloadTmgmtActionApproveForm extends BaseTmgmtActionApproveForm {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'tmgmt_extension_suit_download_multiple_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Download Translation');
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to download translations for these jobs?');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->t('Downloading can take some time, do not close the browser');
  }

  /**
   * Processes the sending batch.
   *
   * @param array $data
   *   Keyed array of data to send.
   * @param array $context
   *   The batch context.
   */
  public static function processBatch($data, &$context) {
    if (!isset($context['results']['errors'])) {
      $context['results']['errors'] = [];
      $context['results']['count'] = 0;
    }
    $entity_type_id = $data['entity_type'];
    $entity_id = $data['entity_id'];
    $job = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->loadMultiple([
      $entity_id,
    ]);
    $job = reset($job);
    if (!$job) {
      $context['results']['errors'][] = t('Entity @entity_type:@entity_id not found', [
        '@entity_type' => $entity_type_id,
        '@entity_id' => $entity_id,
      ]);
    }
    elseif ($translator = $job
      ->getTranslator()) {
      $plugin = $translator
        ->getPlugin();
      if ($plugin instanceof ExtendedTranslatorPluginInterface && $plugin
        ->downloadTranslation($job)) {
        $context['results']['count']++;
      }
      else {
        $context['results']['errors'][] = new FormattableMarkup('Error downloading %name', [
          '%name' => $job
            ->label(),
        ]);
        return;
      }
      $context['message'] = new FormattableMarkup('Processed %name.', [
        '%name' => $job
          ->label(),
      ]);
    }
    else {
      $context['message'] = new FormattableMarkup('Skipped %name.', [
        '%name' => $entity_type_id,
      ]);
    }
  }

}

Classes

Namesort descending Description
DownloadTmgmtActionApproveForm Provides a confirmation form for sending multiple content entities.