annotate core/modules/content_translation/src/Routing/ContentTranslationRouteSubscriber.php @ 0:4c8ae668cc8c

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