NodePreviewController.php in Drupal 8
File
core/modules/node/src/Controller/NodePreviewController.php
View source
<?php
namespace Drupal\node\Controller;
use Drupal\Core\Entity\Controller\EntityViewController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodePreviewController extends EntityViewController {
protected $entityRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) {
parent::__construct($entity_type_manager, $renderer);
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to NodePreviewController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('entity.repository'));
}
public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) {
$node_preview->preview_view_mode = $view_mode_id;
$build = parent::view($node_preview, $view_mode_id);
$build['#attached']['library'][] = 'node/drupal.node.preview';
unset($build['#cache']);
return $build;
}
public function title(EntityInterface $node_preview) {
return $this->entityRepository
->getTranslationFromContext($node_preview)
->label();
}
}