Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\content_translation\Routing\ContentTranslationRouteSubscriber.
*/
namespace Drupal\content_translation\Routing;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
Angie Byron
committed
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Angie Byron
committed
/**
* Subscriber for entity translation routes.
*/
class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
Angie Byron
committed
/**
* The content translation manager.
Angie Byron
committed
*
* @var \Drupal\content_translation\ContentTranslationManagerInterface
Angie Byron
committed
*/
protected $contentTranslationManager;
Angie Byron
committed
/**
* Constructs a ContentTranslationRouteSubscriber object.
*
* @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
* The content translation manager.
Angie Byron
committed
*/
Dries Buytaert
committed
public function __construct(ContentTranslationManagerInterface $content_translation_manager) {
$this->contentTranslationManager = $content_translation_manager;
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
Alex Pott
committed
foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
Dries Buytaert
committed
// Try to get the route from the current collection.
$link_template = $entity_type->getLinkTemplate('canonical');
if (strpos($link_template, '/') !== FALSE) {
$base_path = '/' . $link_template;
}
else {
if (!$entity_route = $collection->get("entity.$entity_type_id.canonical")) {
continue;
}
$base_path = $entity_route->getPath();
}
// Inherit admin route status from edit route, if exists.
$is_admin = FALSE;
$route_name = "entity.$entity_type_id.edit_form";
if ($edit_route = $collection->get($route_name)) {
$is_admin = (bool) $edit_route->getOption('_admin_route');
}
$path = $base_path . '/translations';
$route = new Route(
$path,
array(
catch
committed
'_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
'entity_type_id' => $entity_type_id,
),
array(
'_entity_access' => $entity_type_id . '.view',
Alex Pott
committed
'_access_content_translation_overview' => $entity_type_id,
),
array(
'parameters' => array(
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
),
),
'_admin_route' => $is_admin,
)
);
$route_name = "entity.$entity_type_id.content_translation_overview";
$collection->add($route_name, $route);
Angie Byron
committed
$route = new Route(
$path . '/add/{source}/{target}',
array(
catch
committed
'_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
'source' => NULL,
'target' => NULL,
'_title' => 'Add',
'entity_type_id' => $entity_type_id,
Angie Byron
committed
),
array(
'_entity_access' => $entity_type_id . '.view',
'_access_content_translation_manage' => 'create',
),
array(
'parameters' => array(
'source' => array(
'type' => 'language',
),
'target' => array(
'type' => 'language',
Angie Byron
committed
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
),
Angie Byron
committed
),
'_admin_route' => $is_admin,
)
);
$collection->add("entity.$entity_type_id.content_translation_add", $route);
Alex Pott
committed
$route = new Route(
$path . '/edit/{language}',
array(
catch
committed
'_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
'language' => NULL,
'_title' => 'Edit',
'entity_type_id' => $entity_type_id,
),
array(
'_access_content_translation_manage' => 'update',
),
array(
'parameters' => array(
'language' => array(
'type' => 'language',
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
),
Alex Pott
committed
),
'_admin_route' => $is_admin,
)
);
$collection->add("entity.$entity_type_id.content_translation_edit", $route);
$route = new Route(
$path . '/delete/{language}',
array(
Angie Byron
committed
'_entity_form' => $entity_type_id . '.content_translation_deletion',
'language' => NULL,
'_title' => 'Delete',
'entity_type_id' => $entity_type_id,
),
array(
'_access_content_translation_manage' => 'delete',
),
array(
'parameters' => array(
'language' => array(
'type' => 'language',
Alex Pott
committed
),
$entity_type_id => array(
'type' => 'entity:' . $entity_type_id,
),
),
'_admin_route' => $is_admin,
)
);
$collection->add("entity.$entity_type_id.content_translation_delete", $route);
Angie Byron
committed
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
// Should run after AdminRouteSubscriber so the routes can inherit admin
// status of the edit routes on entities. Therefore priority -210.
$events[RoutingEvents::ALTER] = array('onAlterRoutes', -210);
return $events;
}