3 namespace Drupal\content_translation\Routing;
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;
12 * Subscriber for entity translation routes.
14 class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
17 * The content translation manager.
19 * @var \Drupal\content_translation\ContentTranslationManagerInterface
21 protected $contentTranslationManager;
24 * Constructs a ContentTranslationRouteSubscriber object.
26 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
27 * The content translation manager.
29 public function __construct(ContentTranslationManagerInterface $content_translation_manager) {
30 $this->contentTranslationManager = $content_translation_manager;
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;
44 if (!$entity_route = $collection->get("entity.$entity_type_id.canonical")) {
47 $base_path = $entity_route->getPath();
50 // Inherit admin route status from edit route, if exists.
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');
57 $path = $base_path . '/translations';
62 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
63 'entity_type_id' => $entity_type_id,
66 '_entity_access' => $entity_type_id . '.view',
67 '_access_content_translation_overview' => $entity_type_id,
72 'type' => 'entity:' . $entity_type_id,
75 '_admin_route' => $is_admin,
78 $route_name = "entity.$entity_type_id.content_translation_overview";
79 $collection->add($route_name, $route);
82 $path . '/add/{source}/{target}',
84 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
88 'entity_type_id' => $entity_type_id,
92 '_entity_access' => $entity_type_id . '.view',
93 '_access_content_translation_manage' => 'create',
101 'type' => 'language',
104 'type' => 'entity:' . $entity_type_id,
107 '_admin_route' => $is_admin,
110 $collection->add("entity.$entity_type_id.content_translation_add", $route);
113 $path . '/edit/{language}',
115 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
118 'entity_type_id' => $entity_type_id,
121 '_access_content_translation_manage' => 'update',
126 'type' => 'language',
129 'type' => 'entity:' . $entity_type_id,
132 '_admin_route' => $is_admin,
135 $collection->add("entity.$entity_type_id.content_translation_edit", $route);
138 $path . '/delete/{language}',
140 '_entity_form' => $entity_type_id . '.content_translation_deletion',
142 '_title' => 'Delete',
143 'entity_type_id' => $entity_type_id,
146 '_access_content_translation_manage' => 'delete',
151 'type' => 'language',
154 'type' => 'entity:' . $entity_type_id,
157 '_admin_route' => $is_admin,
160 $collection->add("entity.$entity_type_id.content_translation_delete", $route);
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];