public function ContentTranslationPreviewController::preview

Preview job item entity data.

Parameters

\Drupal\tmgmt\JobItemInterface $tmgmt_job_item: Job item to be previewed.

string $view_mode: The view mode that should be used to display the entity.

Return value

array A render array.

1 string reference to 'ContentTranslationPreviewController::preview'
tmgmt_content.routing.yml in sources/content/tmgmt_content.routing.yml
sources/content/tmgmt_content.routing.yml

File

sources/content/src/Controller/ContentTranslationPreviewController.php, line 56

Class

ContentTranslationPreviewController
Content preview translation controller.

Namespace

Drupal\tmgmt_content\Controller

Code

public function preview(JobItemInterface $tmgmt_job_item, $view_mode) {

  // Load entity.
  $entity = ContentEntitySource::load($tmgmt_job_item
    ->getItemType(), $tmgmt_job_item
    ->getItemId(), $tmgmt_job_item
    ->getJob()
    ->getSourceLangcode());

  // We cannot show the preview for non-existing entities.
  if (!$entity) {
    throw new NotFoundHttpException();
  }
  $data = $tmgmt_job_item
    ->getData();
  $target_langcode = $tmgmt_job_item
    ->getJob()
    ->getTargetLangcode();

  // Populate preview with target translation data.
  $preview = $this
    ->makePreview($entity, $data, $target_langcode);

  // Set the entity into preview mode.
  $preview->in_preview = TRUE;

  // Build view for entity.
  $page = $this->entityTypeManager
    ->getViewBuilder($entity
    ->getEntityTypeId())
    ->view($preview, $view_mode, $preview
    ->language()
    ->getId());

  // The preview is not cacheable.
  $page['#cache']['max-age'] = 0;
  \Drupal::service('page_cache_kill_switch')
    ->trigger();
  return $page;
}