Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\Controller;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Entity\Controller\EntityViewController;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityInterface;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@18
|
9 use Drupal\Core\Render\RendererInterface;
|
Chris@18
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Defines a controller to render a single node in preview.
|
Chris@0
|
14 */
|
Chris@0
|
15 class NodePreviewController extends EntityViewController {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@18
|
18 * The entity repository service.
|
Chris@18
|
19 *
|
Chris@18
|
20 * @var \Drupal\Core\Entity\EntityRepositoryInterface
|
Chris@18
|
21 */
|
Chris@18
|
22 protected $entityRepository;
|
Chris@18
|
23
|
Chris@18
|
24 /**
|
Chris@18
|
25 * Creates an NodeViewController object.
|
Chris@18
|
26 *
|
Chris@18
|
27 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
28 * The entity type manager.
|
Chris@18
|
29 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@18
|
30 * The renderer service.
|
Chris@18
|
31 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@18
|
32 * The entity repository.
|
Chris@18
|
33 */
|
Chris@18
|
34 public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) {
|
Chris@18
|
35 parent::__construct($entity_type_manager, $renderer);
|
Chris@18
|
36 if (!$entity_repository) {
|
Chris@18
|
37 @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);
|
Chris@18
|
38 $entity_repository = \Drupal::service('entity.repository');
|
Chris@18
|
39 }
|
Chris@18
|
40 $this->entityRepository = $entity_repository;
|
Chris@18
|
41 }
|
Chris@18
|
42
|
Chris@18
|
43 /**
|
Chris@18
|
44 * {@inheritdoc}
|
Chris@18
|
45 */
|
Chris@18
|
46 public static function create(ContainerInterface $container) {
|
Chris@18
|
47 return new static(
|
Chris@18
|
48 $container->get('entity_type.manager'),
|
Chris@18
|
49 $container->get('renderer'),
|
Chris@18
|
50 $container->get('entity.repository')
|
Chris@18
|
51 );
|
Chris@18
|
52 }
|
Chris@18
|
53
|
Chris@18
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) {
|
Chris@0
|
58 $node_preview->preview_view_mode = $view_mode_id;
|
Chris@0
|
59 $build = parent::view($node_preview, $view_mode_id);
|
Chris@0
|
60
|
Chris@0
|
61 $build['#attached']['library'][] = 'node/drupal.node.preview';
|
Chris@0
|
62
|
Chris@0
|
63 // Don't render cache previews.
|
Chris@0
|
64 unset($build['#cache']);
|
Chris@0
|
65
|
Chris@0
|
66 return $build;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * The _title_callback for the page that renders a single node in preview.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param \Drupal\Core\Entity\EntityInterface $node_preview
|
Chris@0
|
73 * The current node.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return string
|
Chris@0
|
76 * The page title.
|
Chris@0
|
77 */
|
Chris@0
|
78 public function title(EntityInterface $node_preview) {
|
Chris@18
|
79 return $this->entityRepository->getTranslationFromContext($node_preview)->label();
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 }
|