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 // Inherit admin route status from edit route, if exists.
41 $route_name = "entity.$entity_type_id.edit_form";
42 if ($edit_route = $collection->get($route_name)) {
43 $is_admin = (bool) $edit_route->getOption('_admin_route');
46 $load_latest_revision = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id);
48 if ($entity_type->hasLinkTemplate('drupal:content-translation-overview')) {
50 $entity_type->getLinkTemplate('drupal:content-translation-overview'),
52 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
53 'entity_type_id' => $entity_type_id,
56 '_entity_access' => $entity_type_id . '.view',
57 '_access_content_translation_overview' => $entity_type_id,
62 'type' => 'entity:' . $entity_type_id,
63 'load_latest_revision' => $load_latest_revision,
66 '_admin_route' => $is_admin,
69 $route_name = "entity.$entity_type_id.content_translation_overview";
70 $collection->add($route_name, $route);
73 if ($entity_type->hasLinkTemplate('drupal:content-translation-add')) {
75 $entity_type->getLinkTemplate('drupal:content-translation-add'),
77 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
81 'entity_type_id' => $entity_type_id,
85 '_entity_access' => $entity_type_id . '.view',
86 '_access_content_translation_manage' => 'create',
97 'type' => 'entity:' . $entity_type_id,
98 'load_latest_revision' => $load_latest_revision,
101 '_admin_route' => $is_admin,
104 $collection->add("entity.$entity_type_id.content_translation_add", $route);
107 if ($entity_type->hasLinkTemplate('drupal:content-translation-edit')) {
109 $entity_type->getLinkTemplate('drupal:content-translation-edit'),
111 '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
114 'entity_type_id' => $entity_type_id,
117 '_access_content_translation_manage' => 'update',
122 'type' => 'language',
125 'type' => 'entity:' . $entity_type_id,
126 'load_latest_revision' => $load_latest_revision,
129 '_admin_route' => $is_admin,
132 $collection->add("entity.$entity_type_id.content_translation_edit", $route);
135 if ($entity_type->hasLinkTemplate('drupal:content-translation-delete')) {
137 $entity_type->getLinkTemplate('drupal:content-translation-delete'),
139 '_entity_form' => $entity_type_id . '.content_translation_deletion',
141 '_title' => 'Delete',
142 'entity_type_id' => $entity_type_id,
145 '_access_content_translation_manage' => 'delete',
150 'type' => 'language',
153 'type' => 'entity:' . $entity_type_id,
154 'load_latest_revision' => $load_latest_revision,
157 '_admin_route' => $is_admin,
160 $collection->add("entity.$entity_type_id.content_translation_delete", $route);
163 // Add our custom translation deletion access checker.
164 if ($load_latest_revision) {
165 $entity_delete_route = $collection->get("entity.$entity_type_id.delete_form");
166 if ($entity_delete_route) {
167 $entity_delete_route->addRequirements(['_access_content_translation_delete' => "$entity_type_id.delete"]);
176 public static function getSubscribedEvents() {
177 $events = parent::getSubscribedEvents();
178 // Should run after AdminRouteSubscriber so the routes can inherit admin
179 // status of the edit routes on entities. Therefore priority -210.
180 $events[RoutingEvents::ALTER] = ['onAlterRoutes', -210];