Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/node/src/Controller/NodeController.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
4 | 4 |
5 use Drupal\Component\Utility\Xss; | 5 use Drupal\Component\Utility\Xss; |
6 use Drupal\Core\Controller\ControllerBase; | 6 use Drupal\Core\Controller\ControllerBase; |
7 use Drupal\Core\Datetime\DateFormatterInterface; | 7 use Drupal\Core\Datetime\DateFormatterInterface; |
8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | 8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
9 use Drupal\Core\Entity\EntityRepositoryInterface; | |
9 use Drupal\Core\Render\RendererInterface; | 10 use Drupal\Core\Render\RendererInterface; |
10 use Drupal\Core\Url; | 11 use Drupal\Core\Url; |
11 use Drupal\node\NodeStorageInterface; | 12 use Drupal\node\NodeStorageInterface; |
12 use Drupal\node\NodeTypeInterface; | 13 use Drupal\node\NodeTypeInterface; |
13 use Drupal\node\NodeInterface; | 14 use Drupal\node\NodeInterface; |
31 * @var \Drupal\Core\Render\RendererInterface | 32 * @var \Drupal\Core\Render\RendererInterface |
32 */ | 33 */ |
33 protected $renderer; | 34 protected $renderer; |
34 | 35 |
35 /** | 36 /** |
37 * The entity repository service. | |
38 * | |
39 * @var \Drupal\Core\Entity\EntityRepositoryInterface | |
40 */ | |
41 protected $entityRepository; | |
42 | |
43 /** | |
36 * Constructs a NodeController object. | 44 * Constructs a NodeController object. |
37 * | 45 * |
38 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter | 46 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
39 * The date formatter service. | 47 * The date formatter service. |
40 * @param \Drupal\Core\Render\RendererInterface $renderer | 48 * @param \Drupal\Core\Render\RendererInterface $renderer |
41 * The renderer service. | 49 * The renderer service. |
42 */ | 50 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository |
43 public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) { | 51 * The entity repository. |
52 */ | |
53 public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) { | |
44 $this->dateFormatter = $date_formatter; | 54 $this->dateFormatter = $date_formatter; |
45 $this->renderer = $renderer; | 55 $this->renderer = $renderer; |
56 if (!$entity_repository) { | |
57 @trigger_error('The entity.repository service must be passed to NodeController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); | |
58 $entity_repository = \Drupal::service('entity.repository'); | |
59 } | |
60 $this->entityRepository = $entity_repository; | |
46 } | 61 } |
47 | 62 |
48 /** | 63 /** |
49 * {@inheritdoc} | 64 * {@inheritdoc} |
50 */ | 65 */ |
51 public static function create(ContainerInterface $container) { | 66 public static function create(ContainerInterface $container) { |
52 return new static( | 67 return new static( |
53 $container->get('date.formatter'), | 68 $container->get('date.formatter'), |
54 $container->get('renderer') | 69 $container->get('renderer'), |
70 $container->get('entity.repository') | |
55 ); | 71 ); |
56 } | 72 } |
57 | 73 |
58 /** | 74 /** |
59 * Displays add content links for available content types. | 75 * Displays add content links for available content types. |
68 */ | 84 */ |
69 public function addPage() { | 85 public function addPage() { |
70 $build = [ | 86 $build = [ |
71 '#theme' => 'node_add_list', | 87 '#theme' => 'node_add_list', |
72 '#cache' => [ | 88 '#cache' => [ |
73 'tags' => $this->entityManager()->getDefinition('node_type')->getListCacheTags(), | 89 'tags' => $this->entityTypeManager()->getDefinition('node_type')->getListCacheTags(), |
74 ], | 90 ], |
75 ]; | 91 ]; |
76 | 92 |
77 $content = []; | 93 $content = []; |
78 | 94 |
79 // Only use node types the user has access to. | 95 // Only use node types the user has access to. |
80 foreach ($this->entityManager()->getStorage('node_type')->loadMultiple() as $type) { | 96 foreach ($this->entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) { |
81 $access = $this->entityManager()->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE); | 97 $access = $this->entityTypeManager()->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE); |
82 if ($access->isAllowed()) { | 98 if ($access->isAllowed()) { |
83 $content[$type->id()] = $type; | 99 $content[$type->id()] = $type; |
84 } | 100 } |
85 $this->renderer->addCacheableDependency($build, $access); | 101 $this->renderer->addCacheableDependency($build, $access); |
86 } | 102 } |
104 * | 120 * |
105 * @return array | 121 * @return array |
106 * A node submission form. | 122 * A node submission form. |
107 */ | 123 */ |
108 public function add(NodeTypeInterface $node_type) { | 124 public function add(NodeTypeInterface $node_type) { |
109 $node = $this->entityManager()->getStorage('node')->create([ | 125 $node = $this->entityTypeManager()->getStorage('node')->create([ |
110 'type' => $node_type->id(), | 126 'type' => $node_type->id(), |
111 ]); | 127 ]); |
112 | 128 |
113 $form = $this->entityFormBuilder()->getForm($node); | 129 $form = $this->entityFormBuilder()->getForm($node); |
114 | 130 |
123 * | 139 * |
124 * @return array | 140 * @return array |
125 * An array suitable for \Drupal\Core\Render\RendererInterface::render(). | 141 * An array suitable for \Drupal\Core\Render\RendererInterface::render(). |
126 */ | 142 */ |
127 public function revisionShow($node_revision) { | 143 public function revisionShow($node_revision) { |
128 $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision); | 144 $node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision); |
129 $node = $this->entityManager()->getTranslationFromContext($node); | 145 $node = $this->entityRepository->getTranslationFromContext($node); |
130 $node_view_controller = new NodeViewController($this->entityManager, $this->renderer, $this->currentUser()); | 146 $node_view_controller = new NodeViewController($this->entityTypeManager(), $this->renderer, $this->currentUser(), $this->entityRepository); |
131 $page = $node_view_controller->view($node); | 147 $page = $node_view_controller->view($node); |
132 unset($page['nodes'][$node->id()]['#cache']); | 148 unset($page['nodes'][$node->id()]['#cache']); |
133 return $page; | 149 return $page; |
134 } | 150 } |
135 | 151 |
141 * | 157 * |
142 * @return string | 158 * @return string |
143 * The page title. | 159 * The page title. |
144 */ | 160 */ |
145 public function revisionPageTitle($node_revision) { | 161 public function revisionPageTitle($node_revision) { |
146 $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision); | 162 $node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision); |
147 return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => format_date($node->getRevisionCreationTime())]); | 163 return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => $this->dateFormatter->format($node->getRevisionCreationTime())]); |
148 } | 164 } |
149 | 165 |
150 /** | 166 /** |
151 * Generates an overview table of older revisions of a node. | 167 * Generates an overview table of older revisions of a node. |
152 * | 168 * |
160 $account = $this->currentUser(); | 176 $account = $this->currentUser(); |
161 $langcode = $node->language()->getId(); | 177 $langcode = $node->language()->getId(); |
162 $langname = $node->language()->getName(); | 178 $langname = $node->language()->getName(); |
163 $languages = $node->getTranslationLanguages(); | 179 $languages = $node->getTranslationLanguages(); |
164 $has_translations = (count($languages) > 1); | 180 $has_translations = (count($languages) > 1); |
165 $node_storage = $this->entityManager()->getStorage('node'); | 181 $node_storage = $this->entityTypeManager()->getStorage('node'); |
166 $type = $node->getType(); | 182 $type = $node->getType(); |
167 | 183 |
168 $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $node->label()]) : $this->t('Revisions for %title', ['%title' => $node->label()]); | 184 $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $node->label()]) : $this->t('Revisions for %title', ['%title' => $node->label()]); |
169 $header = [$this->t('Revision'), $this->t('Operations')]; | 185 $header = [$this->t('Revision'), $this->t('Operations')]; |
170 | 186 |
196 $is_current_revision = $vid == $default_revision || (!$current_revision_displayed && $revision->wasDefaultRevision()); | 212 $is_current_revision = $vid == $default_revision || (!$current_revision_displayed && $revision->wasDefaultRevision()); |
197 if (!$is_current_revision) { | 213 if (!$is_current_revision) { |
198 $link = $this->l($date, new Url('entity.node.revision', ['node' => $node->id(), 'node_revision' => $vid])); | 214 $link = $this->l($date, new Url('entity.node.revision', ['node' => $node->id(), 'node_revision' => $vid])); |
199 } | 215 } |
200 else { | 216 else { |
201 $link = $node->link($date); | 217 $link = $node->toLink($date)->toString(); |
202 $current_revision_displayed = TRUE; | 218 $current_revision_displayed = TRUE; |
203 } | 219 } |
204 | 220 |
205 $row = []; | 221 $row = []; |
206 $column = [ | 222 $column = [ |