You are here

protected function GlobalLinkTranslator::getXliffData in GlobalLink Connect for Drupal 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php \Drupal\globallink\Plugin\tmgmt\Translator\GlobalLinkTranslator::getXliffData()

Returns the XLIFF data for a job.

Parameters

\Drupal\tmgmt\JobInterface $job: The translation job.

\Drupal\tmgmt\JobItemInterface[] $job_items: Limit the export to the provided job items.

Return value

array

1 call to GlobalLinkTranslator::getXliffData()
GlobalLinkTranslator::requestJobItemsTranslation in src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php
Requests the translation of a JobItem.

File

src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php, line 740

Class

GlobalLinkTranslator
GlobalLink translation plugin controller.

Namespace

Drupal\globallink\Plugin\tmgmt\Translator

Code

protected function getXliffData(JobInterface $job, array $job_items) {

  // Ensure the job item list is keyed by the job item ID.
  $job_items_by_id = [];
  foreach ($job_items as $job_item) {
    $job_items_by_id[$job_item
      ->id()] = $job_item;
  }
  $conditions['tjiid'] = [
    'value' => array_keys($job_items_by_id),
    'operator' => 'IN',
  ];
  $data = $this->formatManager
    ->createInstance('xlf')
    ->export($job, $conditions);
  $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
  $body = $xml->file->body;
  foreach ($body->group as $group) {
    foreach ($group->{'trans-unit'} as $trans_unit) {
      $trans_unit->target = $trans_unit->source;
    }
  }

  // Added this option for getting the url for every source submitted for
  // translation.
  foreach ($body->group as $group) {
    $job_item_id = (int) $group['id'];
    if ($url = $job_items_by_id[$job_item_id]
      ->getSourceUrl()) {
      $group
        ->addChild('preview', $url
        ->setAbsolute()
        ->toString());
    }
  }
  return $xml
    ->asXML();
}