diff --git a/core/modules/content_translation/content_translation.local_tasks.yml b/core/modules/content_translation/content_translation.local_tasks.yml index 151c584..6e4a6bb 100644 --- a/core/modules/content_translation/content_translation.local_tasks.yml +++ b/core/modules/content_translation/content_translation.local_tasks.yml @@ -1,3 +1,4 @@ content_translation.local_tasks: derivative: 'Drupal\content_translation\Plugin\Derivative\ContentTranslationLocalTasks' + class: 'Drupal\content_translation\Plugin\ContentTranslationLocalTasks' weight: 100 diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationManageAccessCheck.php b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationManageAccessCheck.php index c4c54d4..2c87894 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationManageAccessCheck.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationManageAccessCheck.php @@ -46,8 +46,7 @@ public function appliesTo() { * {@inheritdoc} */ public function access(Route $route, Request $request) { - $entity_type = $request->attributes->get('entity_type'); - if ($entity = $request->attributes->get($entity_type)) { + if ($entity = $request->attributes->get('_entity')) { $route_requirements = $route->getRequirements(); $operation = $route_requirements['_access_content_translation_manage']; $entity_type = $entity->entityType(); diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationOverviewAccess.php b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationOverviewAccess.php index f9dcc1d..ca37e49 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationOverviewAccess.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationOverviewAccess.php @@ -45,8 +45,7 @@ public function appliesTo() { * {@inheritdoc} */ public function access(Route $route, Request $request) { - $entity_type = $request->attributes->get('entity_type'); - if ($entity = $request->attributes->get($entity_type)) { + if ($entity = $request->attributes->get('_entity')) { // Get entity base info. $entity_type = $entity->entityType(); $bundle = $entity->bundle(); diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Controller/ContentTranslationController.php b/core/modules/content_translation/lib/Drupal/content_translation/Controller/ContentTranslationController.php index 21297f5..f94ad33 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Controller/ContentTranslationController.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Controller/ContentTranslationController.php @@ -7,7 +7,7 @@ namespace Drupal\content_translation\Controller; -use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Entity\EntityInterface; /** * Base class for entity translation controllers. @@ -17,28 +17,28 @@ class ContentTranslationController { /** * @todo Remove content_translation_overview(). */ - public function overview(Request $request, $entity_type) { + public function overview(EntityInterface $_entity) { module_load_include('pages.inc', 'content_translation'); - return content_translation_overview($request->attributes->get($entity_type)); + return content_translation_overview($_entity); } /** * @todo Remove content_translation_add_page(). */ - public function add(Request $request, $entity_type, $source, $target) { + public function add(EntityInterface $_entity, $source, $target) { module_load_include('pages.inc', 'content_translation'); $source = language_load($source); $target = language_load($target); - return content_translation_add_page($request->attributes->get($entity_type), $source, $target); + return content_translation_add_page($_entity, $source, $target); } /** * @todo Remove content_translation_edit_page(). */ - public function edit(Request $request, $entity_type, $language) { + public function edit(EntityInterface $_entity, $language) { module_load_include('pages.inc', 'content_translation'); $language = language_load($language); - return content_translation_edit_page($request->attributes->get($entity_type), $language); + return content_translation_edit_page($_entity, $language); } } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Form/ContentTranslationForm.php b/core/modules/content_translation/lib/Drupal/content_translation/Form/ContentTranslationForm.php index 034d220..7142a49 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Form/ContentTranslationForm.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Form/ContentTranslationForm.php @@ -1,5 +1,4 @@ attributes->get($entity_type), $language); + return drupal_get_form('content_translation_delete_confirm', $entity, $language); } } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/ContentTranslationLocalTasks.php b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/ContentTranslationLocalTasks.php new file mode 100644 index 0000000..32bdb57 --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/ContentTranslationLocalTasks.php @@ -0,0 +1,41 @@ +pluginDefinition['entity_type']; + if ($raw_variables = $request->attributes->get('_raw_variables')) { + // When the entity type is in the path, populate '_entity' for any dynamic + // local tasks. + if ($raw_variables->has($entity_type)) { + $entity = $raw_variables->get($entity_type); + $parameters['_entity'] = $entity; + } + // When '_entity' is in the path, populate the parameters with the value + // for the actual entity type. + elseif ($raw_variables->has('_entity')) { + $entity = $raw_variables->get('_entity'); + $parameters[$entity_type] = $entity; + } + } + return $parameters; + } + +} diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php index aa96452..1cc9e66 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php @@ -63,26 +63,27 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) { if ($entity_info['translatable'] && isset($entity_info['translation'])) { $path = '/' . preg_replace('/%(.*)/', '{$1}', $entity_info['menu_base_path']); - $routes = $this->routeProvider->getRoutesByPattern($path)->all(); - if (empty($routes)) { - continue; - } + if ($routes = $this->routeProvider->getRoutesByPattern($path)->all()) { + // Find the route name for the entity page. + $entity_route_name = key($routes); + $entity_tab = $entity_route_name . '_tab'; + // Find the route name for the translation overview. + $translation_route_name = "content_translation.translation_overview_$entity_type"; + $translation_tab = $translation_route_name . '_tab'; - $base_route_name = key($routes); - $tab_root = $base_route_name . '_tab'; - $route_name = "content_translation.translation_overview_$entity_type"; - $tab_name = $route_name . '_tab'; - $this->derivatives[$tab_name] = $base_plugin_definition; - $this->derivatives[$tab_name]['title'] = t('Translate'); - $this->derivatives[$tab_name]['route_name'] = $route_name; - $this->derivatives[$tab_name]['tab_root_id'] = $tab_root; + // Both tabs will have the same root and entity type. + $common_tab_settings = array( + 'tab_root_id' => $entity_tab, + 'entity_type' => $entity_type, + ); + $this->derivatives[$entity_tab] = $base_plugin_definition + $common_tab_settings; + $this->derivatives[$entity_tab]['title'] = t('Edit'); + $this->derivatives[$entity_tab]['route_name'] = $entity_route_name; - // @todo Remove after https://drupal.org/node/2095117. - $tab_name = $tab_root; - $this->derivatives[$tab_name] = $base_plugin_definition; - $this->derivatives[$tab_name]['title'] = t('Edit'); - $this->derivatives[$tab_name]['route_name'] = $base_route_name; - $this->derivatives[$tab_name]['tab_root_id'] = $tab_root; + $this->derivatives[$translation_tab] = $base_plugin_definition + $common_tab_settings; + $this->derivatives[$translation_tab]['title'] = t('Translate'); + $this->derivatives[$translation_tab]['route_name'] = $translation_route_name; + } } } return parent::getDerivativeDefinitions($base_plugin_definition); diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php b/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php index e3271df..fb66a24 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php @@ -50,18 +50,24 @@ public function routes(RouteBuildEvent $event) { $collection = $event->getRouteCollection(); foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) { if ($entity_info['translatable'] && isset($entity_info['translation'])) { - $path = '/' . preg_replace('/%(.*)/', '{$1}', $entity_info['menu_base_path']) . '/translations'; + $path = '/' . str_replace($entity_info['menu_path_wildcard'], '{_entity}', $entity_info['menu_base_path']) . '/translations'; $route = new Route( $path, array( '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::overview', '_title' => 'Translate', 'account' => 'NULL', - 'entity_type' => $entity_type, ), array( '_access_content_translation_overview' => $entity_type, '_permission' => 'translate any entity', + ), + array( + 'parameters' => array( + '_entity' => array( + 'type' => 'entity:' . $entity_type, + ), + ), ) ); $collection->add("content_translation.translation_overview_$entity_type", $route); @@ -73,11 +79,18 @@ public function routes(RouteBuildEvent $event) { 'source' => NULL, 'target' => NULL, '_title' => 'Add', - 'entity_type' => $entity_type, + ), array( '_permission' => 'translate any entity', '_access_content_translation_manage' => 'create', + ), + array( + 'parameters' => array( + '_entity' => array( + 'type' => 'entity:' . $entity_type, + ), + ), ) ); $collection->add("content_translation.translation_add_$entity_type", $route); @@ -88,11 +101,17 @@ public function routes(RouteBuildEvent $event) { '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::edit', 'language' => NULL, '_title' => 'Edit', - 'entity_type' => $entity_type, ), array( '_permission' => 'translate any entity', '_access_content_translation_manage' => 'update', + ), + array( + 'parameters' => array( + '_entity' => array( + 'type' => 'entity:' . $entity_type, + ), + ), ) ); $collection->add("content_translation.translation_edit_$entity_type", $route); @@ -103,11 +122,17 @@ public function routes(RouteBuildEvent $event) { '_content' => '\Drupal\content_translation\Form\ContentTranslationForm::deleteTranslation', 'language' => NULL, '_title' => 'Delete', - 'entity_type' => $entity_type, ), array( '_permission' => 'translate any entity', '_access_content_translation_manage' => 'delete', + ), + array( + 'parameters' => array( + '_entity' => array( + 'type' => 'entity:' . $entity_type, + ), + ), ) ); $collection->add("content_translation.delete_$entity_type", $route); diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Controller/EntityTestController.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Controller/EntityTestController.php index 03d7dc0..09e604b 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Controller/EntityTestController.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Controller/EntityTestController.php @@ -7,7 +7,7 @@ namespace Drupal\entity_test\Controller; -use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Entity\EntityInterface; /** * Controller routines for entity_test routes. @@ -24,8 +24,8 @@ public function testAdd($entity_type) { /** * @todo Remove entity_test_edit() */ - public function testEdit(Request $request, $entity_type) { - return entity_test_edit($request->attributes->get($entity_type)); + public function testEdit(EntityInterface $_entity) { + return entity_test_edit($_entity); } } diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php index 3a8c734..0834cc1 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php @@ -42,9 +42,12 @@ public function routes(RouteBuildEvent $event) { $collection->add("entity_test.add_$entity_type", $route); $route = new Route( - "$entity_type/manage/{" . $entity_type . '}', - array('_content' => '\Drupal\entity_test\Controller\EntityTestController::testEdit', 'entity_type' => $entity_type), - array('_permission' => 'administer entity_test content') + "$entity_type/manage/{_entity}", + array('_content' => '\Drupal\entity_test\Controller\EntityTestController::testEdit'), + array('_permission' => 'administer entity_test content'), + array('parameters' => array( + '_entity' => array('type' => 'entity:' . $entity_type), + )) ); $collection->add("entity_test.edit_$entity_type", $route); }