ContentTranslationPreviewController.php in Translation Management Tool 8
File
sources/content/src/Controller/ContentTranslationPreviewController.php
View source
<?php
namespace Drupal\tmgmt_content\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\Core\Render\Element;
use Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ContentTranslationPreviewController extends ControllerBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function preview(JobItemInterface $tmgmt_job_item, $view_mode) {
$entity = ContentEntitySource::load($tmgmt_job_item
->getItemType(), $tmgmt_job_item
->getItemId(), $tmgmt_job_item
->getJob()
->getSourceLangcode());
if (!$entity) {
throw new NotFoundHttpException();
}
$data = $tmgmt_job_item
->getData();
$target_langcode = $tmgmt_job_item
->getJob()
->getTargetLangcode();
$preview = $this
->makePreview($entity, $data, $target_langcode);
$preview->in_preview = TRUE;
$page = $this->entityTypeManager
->getViewBuilder($entity
->getEntityTypeId())
->view($preview, $view_mode, $preview
->language()
->getId());
$page['#cache']['max-age'] = 0;
\Drupal::service('page_cache_kill_switch')
->trigger();
return $page;
}
public function title(JobItemInterface $tmgmt_job_item) {
$target_language = $tmgmt_job_item
->getJob()
->getTargetLanguage()
->getName();
$entity = ContentEntitySource::load($tmgmt_job_item
->getItemType(), $tmgmt_job_item
->getItemId(), $tmgmt_job_item
->getJob()
->getSourceLangcode());
$title = $entity
->label();
return t("Preview of @title for @target_language", [
'@title' => $title,
'@target_language' => $target_language,
]);
}
protected function makePreview(ContentEntityInterface $entity, array $data, $target_langcode) {
if (!$entity
->hasTranslation($target_langcode)) {
$entity
->addTranslation($target_langcode, $entity
->toArray());
}
$embeded_fields = $this
->config('tmgmt_content.settings')
->get('embedded_fields');
$translation = $entity
->getTranslation($target_langcode);
foreach (Element::children($data) as $name) {
$field_data = $data[$name];
foreach (Element::children($field_data) as $delta) {
$field_item = $field_data[$delta];
foreach (Element::children($field_item) as $property) {
$property_data = $field_item[$property];
if (isset($property_data['#translation']['#text']) && $property_data['#translate'] && is_numeric($delta)) {
$item = $translation
->get($name)
->offsetGet($delta);
if ($item) {
$translation
->get($name)
->offsetGet($delta)
->set($property, $property_data['#translation']['#text']);
}
}
elseif (isset($embeded_fields[$entity
->getEntityTypeId()][$name])) {
$this
->makePreview($translation
->get($name)
->offsetGet($delta)->{$property}, $property_data, $target_langcode);
}
}
}
}
return $translation;
}
}