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