3 namespace Drupal\content_translation\Routing;
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;
13 * Subscriber for entity translation routes.
15 class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
18 * The content translation manager.
20 * @var \Drupal\content_translation\ContentTranslationManagerInterface
22 protected $contentTranslationManager;
25 * Constructs a ContentTranslationRouteSubscriber object.
27 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
28 * The content translation manager.
30 public function __construct(ContentTranslationManagerInterface $content_translation_manager) {
31 $this->contentTranslationManager = $content_translation_manager;
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;
45 if (!$entity_route = $collection->get("entity.$entity_type_id.canonical")) {
48 $base_path = $entity_route->getPath();
51 // Inherit admin route status from edit route, if exists.
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');
58 $path = $base_path . '/translations';
59 $load_latest_revision = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id);
64 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
65 'entity_type_id' => $entity_type_id,
68 '_entity_access' => $entity_type_id . '.view',
69 '_access_content_translation_overview' => $entity_type_id,
74 'type' => 'entity:' . $entity_type_id,
75 'load_latest_revision' => $load_latest_revision,
78 '_admin_route' => $is_admin,
81 $route_name = "entity.$entity_type_id.content_translation_overview";
82 $collection->add($route_name, $route);
85 $path . '/add/{source}/{target}',
87 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
91 'entity_type_id' => $entity_type_id,
95 '_entity_access' => $entity_type_id . '.view',
96 '_access_content_translation_manage' => 'create',
101 'type' => 'language',
104 'type' => 'language',
107 'type' => 'entity:' . $entity_type_id,
108 'load_latest_revision' => $load_latest_revision,
111 '_admin_route' => $is_admin,
114 $collection->add("entity.$entity_type_id.content_translation_add", $route);
117 $path . '/edit/{language}',
119 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
122 'entity_type_id' => $entity_type_id,
125 '_access_content_translation_manage' => 'update',
130 'type' => 'language',
133 'type' => 'entity:' . $entity_type_id,
134 'load_latest_revision' => $load_latest_revision,
137 '_admin_route' => $is_admin,
140 $collection->add("entity.$entity_type_id.content_translation_edit", $route);
143 $path . '/delete/{language}',
145 '_entity_form' => $entity_type_id . '.content_translation_deletion',
147 '_title' => 'Delete',
148 'entity_type_id' => $entity_type_id,
151 '_access_content_translation_manage' => 'delete',
156 'type' => 'language',
159 'type' => 'entity:' . $entity_type_id,
160 'load_latest_revision' => $load_latest_revision,
163 '_admin_route' => $is_admin,
166 $collection->add("entity.$entity_type_id.content_translation_delete", $route);
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"]);
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];