Updated to Drupal 8.5. Core Media not yet in use.
[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       // Try to get the route from the current collection.
40       $link_template = $entity_type->getLinkTemplate('canonical');
41       if (strpos($link_template, '/') !== FALSE) {
42         $base_path = '/' . $link_template;
43       }
44       else {
45         if (!$entity_route = $collection->get("entity.$entity_type_id.canonical")) {
46           continue;
47         }
48         $base_path = $entity_route->getPath();
49       }
50
51       // Inherit admin route status from edit route, if exists.
52       $is_admin = FALSE;
53       $route_name = "entity.$entity_type_id.edit_form";
54       if ($edit_route = $collection->get($route_name)) {
55         $is_admin = (bool) $edit_route->getOption('_admin_route');
56       }
57
58       $path = $base_path . '/translations';
59       $load_latest_revision = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id);
60
61       $route = new Route(
62         $path,
63         [
64           '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
65           'entity_type_id' => $entity_type_id,
66         ],
67         [
68           '_entity_access' => $entity_type_id . '.view',
69           '_access_content_translation_overview' => $entity_type_id,
70         ],
71         [
72           'parameters' => [
73             $entity_type_id => [
74               'type' => 'entity:' . $entity_type_id,
75               'load_latest_revision' => $load_latest_revision,
76             ],
77           ],
78           '_admin_route' => $is_admin,
79         ]
80       );
81       $route_name = "entity.$entity_type_id.content_translation_overview";
82       $collection->add($route_name, $route);
83
84       $route = new Route(
85         $path . '/add/{source}/{target}',
86         [
87           '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
88           'source' => NULL,
89           'target' => NULL,
90           '_title' => 'Add',
91           'entity_type_id' => $entity_type_id,
92
93         ],
94         [
95           '_entity_access' => $entity_type_id . '.view',
96           '_access_content_translation_manage' => 'create',
97         ],
98         [
99           'parameters' => [
100             'source' => [
101               'type' => 'language',
102             ],
103             'target' => [
104               'type' => 'language',
105             ],
106             $entity_type_id => [
107               'type' => 'entity:' . $entity_type_id,
108               'load_latest_revision' => $load_latest_revision,
109             ],
110           ],
111           '_admin_route' => $is_admin,
112         ]
113       );
114       $collection->add("entity.$entity_type_id.content_translation_add", $route);
115
116       $route = new Route(
117         $path . '/edit/{language}',
118         [
119           '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
120           'language' => NULL,
121           '_title' => 'Edit',
122           'entity_type_id' => $entity_type_id,
123         ],
124         [
125           '_access_content_translation_manage' => 'update',
126         ],
127         [
128           'parameters' => [
129             'language' => [
130               'type' => 'language',
131             ],
132             $entity_type_id => [
133               'type' => 'entity:' . $entity_type_id,
134               'load_latest_revision' => $load_latest_revision,
135             ],
136           ],
137           '_admin_route' => $is_admin,
138         ]
139       );
140       $collection->add("entity.$entity_type_id.content_translation_edit", $route);
141
142       $route = new Route(
143         $path . '/delete/{language}',
144         [
145           '_entity_form' => $entity_type_id . '.content_translation_deletion',
146           'language' => NULL,
147           '_title' => 'Delete',
148           'entity_type_id' => $entity_type_id,
149         ],
150         [
151           '_access_content_translation_manage' => 'delete',
152         ],
153         [
154           'parameters' => [
155             'language' => [
156               'type' => 'language',
157             ],
158             $entity_type_id => [
159               'type' => 'entity:' . $entity_type_id,
160               'load_latest_revision' => $load_latest_revision,
161             ],
162           ],
163           '_admin_route' => $is_admin,
164         ]
165       );
166       $collection->add("entity.$entity_type_id.content_translation_delete", $route);
167
168       // Add our custom translation deletion access checker.
169       if ($load_latest_revision) {
170         $entity_delete_route = $collection->get("entity.$entity_type_id.delete_form");
171         if ($entity_delete_route) {
172           $entity_delete_route->addRequirements(['_access_content_translation_delete' => "$entity_type_id.delete"]);
173         }
174       }
175     }
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public static function getSubscribedEvents() {
182     $events = parent::getSubscribedEvents();
183     // Should run after AdminRouteSubscriber so the routes can inherit admin
184     // status of the edit routes on entities. Therefore priority -210.
185     $events[RoutingEvents::ALTER] = ['onAlterRoutes', -210];
186     return $events;
187   }
188
189 }