annotate core/modules/content_translation/src/Routing/ContentTranslationRouteSubscriber.php @ 14:1fec387a4317

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