diff --git a/sources/content/src/Controller/ContentTranslationPreviewController.php b/sources/content/src/Controller/ContentTranslationPreviewController.php index 90e5a16..1241bab 100644 --- a/sources/content/src/Controller/ContentTranslationPreviewController.php +++ b/sources/content/src/Controller/ContentTranslationPreviewController.php @@ -7,7 +7,7 @@ namespace Drupal\tmgmt_content\Controller; -use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\tmgmt\JobItemInterface; @@ -17,23 +17,23 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Content preview translation controller. */ -class ContentTranslationPreviewController implements ContainerInjectionInterface { +class ContentTranslationPreviewController extends ControllerBase { /** - * The entity manager. + * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; /** * Creates an ContentTranslationPreviewController object. * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(EntityTypeManagerInterface $entity_manager) { - $this->entityManager = $entity_manager; + public function __construct(EntityTypeManagerInterface $entity_type_manager) { + $this->entityTypeManager = $entity_type_manager; } /** @@ -46,19 +46,19 @@ class ContentTranslationPreviewController implements ContainerInjectionInterface } /** - * Preview entity data of job item. + * Preview job item entity data. * * @param \Drupal\tmgmt\JobItemInterface $tmgmt_job_item * Job item to be previewed. - * @param string $preview_type - * The preview mode that should be used to display the entity. + * @param string $view_mode + * The view mode that should be used to display the entity. * * @return array * A render array as expected by drupal_render(). */ - public function preview(JobItemInterface $tmgmt_job_item, $preview_type = 'full') { + public function preview(JobItemInterface $tmgmt_job_item, $view_mode) { // Load entity. - $entity = $this->entityManager + $entity = $this->entityTypeManager ->getStorage($tmgmt_job_item->getItemType()) ->load($tmgmt_job_item->getItemId()); $data = $tmgmt_job_item->getData(); @@ -66,9 +66,9 @@ class ContentTranslationPreviewController implements ContainerInjectionInterface // Populate preview with target translation data. $preview = $this->makePreview($entity, $data, $target_langcode); // Build view for entity. - $page = $this->entityManager + $page = $this->entityTypeManager ->getViewBuilder($entity->getEntityTypeId()) - ->view($preview, $preview_type); + ->view($preview, $view_mode, $preview->language()->getId()); return $page; } @@ -83,28 +83,29 @@ class ContentTranslationPreviewController implements ContainerInjectionInterface * The page title. */ public function title(JobItemInterface $tmgmt_job_item) { - $target_langcode = $tmgmt_job_item->getJob()->getTargetLangcode(); - $title = $this->entityManager + $target_language = $tmgmt_job_item->getJob()->getTargetLanguage()->getName(); + $title = $this->entityTypeManager ->getStorage($tmgmt_job_item->getItemType()) ->load($tmgmt_job_item->getItemId()) ->getTitle(); - return t("Preview of @title for @target_langcode", [ + return t("Preview of @title for @target_language", [ '@title' => $title, - '@target_langcode' => $target_langcode, + '@target_language' => $target_language, ]); } /** - * Return translation data. + * Builds the entity translation for the provided translation data. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity for which the translation should be returned. * @param array $data * The translation data for the fields. - * @param int $target_langcode + * @param string $target_langcode * The target language. * - * @return $this + * @return \Drupal\Core\Entity\ContentEntityInterface $translation + * Translation data. */ protected function makePreview(ContentEntityInterface $entity, array $data, $target_langcode) { // If the translation for this language does not exist yet, initialize it. @@ -112,7 +113,7 @@ class ContentTranslationPreviewController implements ContainerInjectionInterface $entity->addTranslation($target_langcode, $entity->toArray()); } - $embeded_fields = \Drupal::config('tmgmt_content.settings')->get('embedded_fields'); + $embeded_fields = $this->config('tmgmt_content.settings')->get('embedded_fields'); $translation = $entity->getTranslation($target_langcode); diff --git a/sources/content/src/Tests/ContentEntitySourceUiTest.php b/sources/content/src/Tests/ContentEntitySourceUiTest.php index 7f86873..5c1c43a 100644 --- a/sources/content/src/Tests/ContentEntitySourceUiTest.php +++ b/sources/content/src/Tests/ContentEntitySourceUiTest.php @@ -480,19 +480,25 @@ class ContentEntitySourceUiTest extends EntityTestBase { $job = $this->createJob('en', 'de'); $job->translator = $this->default_translator->id(); - $job->settings = array(); $job->save(); - - $job->addItem('content', 'node', $node->id()); + $job_item = tmgmt_job_item_create('content', $node->getEntityTypeId(), $node->id(), array('tjid' => $job->id())); + $job_item->save(); $job->requestTranslation(); + $items = $job->getItems(); + $item = reset($items); + $item->acceptTranslation(); + // Visit preview route. $this->drupalGet(URL::fromRoute('entity.tmgmt_job_item.preview', ['tmgmt_job_item' => $job->id()])->setAbsolute()->toString()); $this->assertResponse(200); - $this->assertTitle(t("Preview of @title for @target_langcode | Drupal", [ + $this->assertTitle(t("Preview of @title for @target_language | Drupal", [ '@title' => $node->getTitle(), - '@target_langcode' => $job->getTargetLangcode(), + '@target_language' => $job->getTargetLanguage()->getName(), ])); - $this->assertText($node->getTitle()); + + $translated_node = entity_load($node->getEntityTypeId(), $node->id()); + $de_node = $translated_node->getTranslation('de'); + $this->assertText($de_node->getTitle()); } } diff --git a/sources/content/tmgmt_content.routing.yml b/sources/content/tmgmt_content.routing.yml index 5b370c8..b933fe9 100644 --- a/sources/content/tmgmt_content.routing.yml +++ b/sources/content/tmgmt_content.routing.yml @@ -1,8 +1,9 @@ entity.tmgmt_job_item.preview: - path: '/admin/tmgmt/content/preview/{tmgmt_job_item}' + path: '/tmgmt/content/preview/{tmgmt_job_item}/{view_mode}' defaults: _controller: '\Drupal\tmgmt_content\Controller\ContentTranslationPreviewController::preview' _title_callback: '\Drupal\tmgmt_content\Controller\ContentTranslationPreviewController::title' + view_mode: 'full' requirements: _permission: 'accept translation jobs' options: