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