6cd3b350dfe80009660fc218409b9e638e5d4a0a
[yaffs-website] / web / core / modules / content_translation / src / Routing / ContentTranslationRouteSubscriber.php
1 <?php
2
3 namespace Drupal\content_translation\Routing;
4
5 use Drupal\content_translation\ContentTranslationManager;
6 use Drupal\content_translation\ContentTranslationManagerInterface;
7 use Drupal\Core\Routing\RouteSubscriberBase;
8 use Drupal\Core\Routing\RoutingEvents;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
11
12 /**
13  * Subscriber for entity translation routes.
14  */
15 class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
16
17   /**
18    * The content translation manager.
19    *
20    * @var \Drupal\content_translation\ContentTranslationManagerInterface
21    */
22   protected $contentTranslationManager;
23
24   /**
25    * Constructs a ContentTranslationRouteSubscriber object.
26    *
27    * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
28    *   The content translation manager.
29    */
30   public function __construct(ContentTranslationManagerInterface $content_translation_manager) {
31     $this->contentTranslationManager = $content_translation_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function alterRoutes(RouteCollection $collection) {
38     foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
39       // Inherit admin route status from edit route, if exists.
40       $is_admin = FALSE;
41       $route_name = "entity.$entity_type_id.edit_form";
42       if ($edit_route = $collection->get($route_name)) {
43         $is_admin = (bool) $edit_route->getOption('_admin_route');
44       }
45
46       $load_latest_revision = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id);
47
48       if ($entity_type->hasLinkTemplate('drupal:content-translation-overview')) {
49         $route = new Route(
50           $entity_type->getLinkTemplate('drupal:content-translation-overview'),
51           [
52             '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
53             'entity_type_id' => $entity_type_id,
54           ],
55           [
56             '_entity_access' => $entity_type_id . '.view',
57             '_access_content_translation_overview' => $entity_type_id,
58           ],
59           [
60             'parameters' => [
61               $entity_type_id => [
62                 'type' => 'entity:' . $entity_type_id,
63                 'load_latest_revision' => $load_latest_revision,
64               ],
65             ],
66             '_admin_route' => $is_admin,
67           ]
68         );
69         $route_name = "entity.$entity_type_id.content_translation_overview";
70         $collection->add($route_name, $route);
71       }
72
73       if ($entity_type->hasLinkTemplate('drupal:content-translation-add')) {
74         $route = new Route(
75           $entity_type->getLinkTemplate('drupal:content-translation-add'),
76           [
77             '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
78             'source' => NULL,
79             'target' => NULL,
80             '_title' => 'Add',
81             'entity_type_id' => $entity_type_id,
82
83           ],
84           [
85             '_entity_access' => $entity_type_id . '.view',
86             '_access_content_translation_manage' => 'create',
87           ],
88           [
89             'parameters' => [
90               'source' => [
91                 'type' => 'language',
92               ],
93               'target' => [
94                 'type' => 'language',
95               ],
96               $entity_type_id => [
97                 'type' => 'entity:' . $entity_type_id,
98                 'load_latest_revision' => $load_latest_revision,
99               ],
100             ],
101             '_admin_route' => $is_admin,
102           ]
103         );
104         $collection->add("entity.$entity_type_id.content_translation_add", $route);
105       }
106
107       if ($entity_type->hasLinkTemplate('drupal:content-translation-edit')) {
108         $route = new Route(
109           $entity_type->getLinkTemplate('drupal:content-translation-edit'),
110           [
111             '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
112             'language' => NULL,
113             '_title' => 'Edit',
114             'entity_type_id' => $entity_type_id,
115           ],
116           [
117             '_access_content_translation_manage' => 'update',
118           ],
119           [
120             'parameters' => [
121               'language' => [
122                 'type' => 'language',
123               ],
124               $entity_type_id => [
125                 'type' => 'entity:' . $entity_type_id,
126                 'load_latest_revision' => $load_latest_revision,
127               ],
128             ],
129             '_admin_route' => $is_admin,
130           ]
131         );
132         $collection->add("entity.$entity_type_id.content_translation_edit", $route);
133       }
134
135       if ($entity_type->hasLinkTemplate('drupal:content-translation-delete')) {
136         $route = new Route(
137           $entity_type->getLinkTemplate('drupal:content-translation-delete'),
138           [
139             '_entity_form' => $entity_type_id . '.content_translation_deletion',
140             'language' => NULL,
141             '_title' => 'Delete',
142             'entity_type_id' => $entity_type_id,
143           ],
144           [
145             '_access_content_translation_manage' => 'delete',
146           ],
147           [
148             'parameters' => [
149               'language' => [
150                 'type' => 'language',
151               ],
152               $entity_type_id => [
153                 'type' => 'entity:' . $entity_type_id,
154                 'load_latest_revision' => $load_latest_revision,
155               ],
156             ],
157             '_admin_route' => $is_admin,
158           ]
159         );
160         $collection->add("entity.$entity_type_id.content_translation_delete", $route);
161       }
162
163       // Add our custom translation deletion access checker.
164       if ($load_latest_revision) {
165         $entity_delete_route = $collection->get("entity.$entity_type_id.delete_form");
166         if ($entity_delete_route) {
167           $entity_delete_route->addRequirements(['_access_content_translation_delete' => "$entity_type_id.delete"]);
168         }
169       }
170     }
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   public static function getSubscribedEvents() {
177     $events = parent::getSubscribedEvents();
178     // Should run after AdminRouteSubscriber so the routes can inherit admin
179     // status of the edit routes on entities. Therefore priority -210.
180     $events[RoutingEvents::ALTER] = ['onAlterRoutes', -210];
181     return $events;
182   }
183
184 }