diff --git a/config_translation.module b/config_translation.module index 58d1e1c..6b75adf 100644 --- a/config_translation.module +++ b/config_translation.module @@ -67,17 +67,17 @@ function config_translation_menu() { $depth = substr_count($path, '/'); $items[$path . '/translate/add/%language'] = array( 'title' => 'Translate', - 'route_name' => $group->getRouterName() . '_add', + 'route_name' => $group->getRouterName() . '.add', 'type' => MENU_CALLBACK, ); $items[$path . '/translate/edit/%language'] = array( 'title' => 'Translate', - 'route_name' => $group->getRouterName() . '_edit', + 'route_name' => $group->getRouterName() . '.edit', 'type' => MENU_CALLBACK, ); $items[$path . '/translate/delete/%language'] = array( 'title' => 'Delete', - 'route_name' => $group->getRouterName() . '_delete', + 'route_name' => $group->getRouterName() . '.delete', 'type' => MENU_CALLBACK, ); } @@ -219,14 +219,14 @@ function config_translation_exists($name, Language $language) { * can be expanded. */ function config_translation_get_groups() { - $config_groups = drupal_container()->get('module_handler')->invokeAll('config_translation_group_info'); + $config_groups = Drupal::ModuleHandler()->invokeAll('config_translation_group_info'); // Create an array of path indexed groups for easier altering. $path_indexed_groups = array(); foreach ($config_groups as $group) { $path_indexed_groups[$group->getBasePath()] = $group; } - drupal_container()->get('module_handler')->alter('config_translation_group_info', $path_indexed_groups); + Drupal::ModuleHandler()->alter('config_translation_group_info', $path_indexed_groups); return $path_indexed_groups; } diff --git a/lib/Drupal/config_translation/ConfigEntityMapper.php b/lib/Drupal/config_translation/ConfigEntityMapper.php index 1f5d431..c90ea24 100644 --- a/lib/Drupal/config_translation/ConfigEntityMapper.php +++ b/lib/Drupal/config_translation/ConfigEntityMapper.php @@ -167,7 +167,9 @@ class ConfigEntityMapper implements ConfigMapperInterface { * {@inheritdoc} */ public function setRouteName() { - $this->router_name = 'config_translation.item.' . str_replace(array('/', '-'), array('.', '_'), $this->base_path); + $search = array('/', '-', '{', '}'); + $replace = array('.', '_', '_', '_'); + $this->router_name = 'config_translation.item.' . str_replace($search, $replace, $this->base_path); } /** diff --git a/lib/Drupal/config_translation/Controller/ConfigTranslationController.php b/lib/Drupal/config_translation/Controller/ConfigTranslationController.php index e7e1b69..b55689a 100644 --- a/lib/Drupal/config_translation/Controller/ConfigTranslationController.php +++ b/lib/Drupal/config_translation/Controller/ConfigTranslationController.php @@ -7,6 +7,7 @@ namespace Drupal\config_translation\Controller; +use Drupal\Core\TypedData\Type\Language; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Drupal\Core\ControllerInterface; @@ -14,6 +15,7 @@ use Drupal\config_translation\ConfigGroupMapper; use Drupal\config_translation\ConfigMapperInterface; use Drupal\Core\Config\Config; use Drupal\config_translation\Form\ConfigTranslationManageForm; +use Drupal\config_translation\Form\ConfigTranslationDeleteForm; /** * Controller providing page callbacks for the config translation interface. @@ -41,7 +43,7 @@ class ConfigTranslationController implements ControllerInterface { '#empty' => t('No translatable configuration found.'), ); - $config_groups = self::getTranslationGroups(); + $config_groups = config_translation_get_groups(); foreach ($config_groups as $group) { if ($group instanceof ConfigGroupMapper && $group->hasSchema() && $group->hasTranslatable()) { // Figure out language code and name for this configuration. Shipped @@ -76,41 +78,18 @@ class ConfigTranslationController implements ControllerInterface { } /** - * Get group definitions from hooks and make it possible to alter groups. - * - * Configuration groups are used to get multiple configuration names used for - * one specific configuration form together. If contributed modules alter a - * form adding in additional settings stored elsewhere, the list of names - * can be expanded. - */ - public static function getTranslationGroups() { - $config_groups = \Drupal::ModuleHandler()->invokeAll('config_translation_group_info'); - - // Create an array of path indexed groups for easier altering. - $path_indexed_groups = array(); - foreach ($config_groups as $group) { - $path_indexed_groups[$group->getBasePath()] = $group; - } - \Drupal::ModuleHandler()->alter('config_translation_group_info', $path_indexed_groups); - return $path_indexed_groups; - } - - /** * Language translations overview page for a configuration name. * + ** @param Request $request + * Page request object. * @param ConfigMapperInterface $mapper * Configuration mapper. - * @param mixed $path_arg - * Path argument from the menu system (entity id or loaded entity for - * configuration entities, NULL otherwise). * * @return array * Page render array. */ public function itemOverviewPage(Request $request, ConfigMapperInterface $mapper) { - // Get configuration group for this mapper. - $entity = $request->attributes->get($mapper->getType()); - $group = $mapper->getConfigGroup($entity); + $group = $this->getConfigGroup($request, $mapper); drupal_set_title(t('Translations for %label', array('%label' => $group->getTitle())), PASS_THROUGH); // It is possible the original language this configuration was saved with is @@ -187,28 +166,22 @@ class ConfigTranslationController implements ControllerInterface { } /** + * Renders translation item manage form. + * * @param Request $request + * Page request object. * @param string $action * Action identifier, either 'add' or 'edit'. Used to provide proper * labeling on the screen. * @param ConfigMapperInterface $mapper * Configuration mapper. - * @param mixed $path_arg - * Path argument from the menu system (entity id or loaded entity for - * configuration entities, NULL otherwise). - * @param Language $language - * A language object. * * @return array|mixed */ - function itemTranslatePage(Request $request, $action, ConfigMapperInterface $mapper) { - // Get configuration group for this mapper. - $entity = $request->attributes->get($mapper->getType()); - $group = $mapper->getConfigGroup($entity); - $langcode = $request->attributes->get('langcode'); - if ($langcode) { - $language = language_load($langcode); - } + public function itemTranslatePage(Request $request, $action, ConfigMapperInterface $mapper) { + $group = $this->getConfigGroup($request, $mapper); + $language = $this->getLanguage($request, $mapper); + $replacement = array( '%label' => $group->getTitle(), '@language' => strtolower($language->name), @@ -241,4 +214,45 @@ class ConfigTranslationController implements ControllerInterface { return drupal_get_form(new ConfigTranslationManageForm(), $group, $language, $base_config); } + /** + * Item delete form. + * + * @param Request $request + * @param ConfigMapperInterface $mapper + * @return array|mixed + */ + public function itemDeletePage(Request $request, ConfigMapperInterface $mapper) { + return drupal_get_form(new ConfigTranslationDeleteForm(), $this->getConfigGroup($request, $mapper), $this->getLanguage($request, $mapper)); + } + + /** + * Helper to get config group. + * + * @param Request $request + * @param ConfigMapperInterface $mapper + * + * @return ConfigGroupMapper + */ + protected function getConfigGroup(Request $request, ConfigMapperInterface $mapper) { + // Get configuration group for this mapper. + $entity = $request->attributes->get($mapper->getType()); + return $mapper->getConfigGroup($entity); + } + + /** + * Helper to get language object. + * + * @param Request $request + * @param ConfigMapperInterface $mapper + * + * @return bool|\Drupal\core\Language\Language + */ + protected function getLanguage(Request $request, ConfigMapperInterface $mapper) { + $langcode = $request->attributes->get('langcode'); + if ($langcode) { + return language_load($langcode); + } + return FALSE; + } + } diff --git a/lib/Drupal/config_translation/Form/ConfigTranslationDeleteForm.php b/lib/Drupal/config_translation/Form/ConfigTranslationDeleteForm.php index 06f5fec..3a4beed 100644 --- a/lib/Drupal/config_translation/Form/ConfigTranslationDeleteForm.php +++ b/lib/Drupal/config_translation/Form/ConfigTranslationDeleteForm.php @@ -10,6 +10,7 @@ namespace Drupal\config_translation\Form; use Drupal\Core\Form\ConfirmFormBase; use Symfony\Component\HttpFoundation\Request; use Drupal\config_translation\ConfigMapperInterface; +use Symfony\Component\Routing\Route; /** * Builds a form to delete an action. @@ -30,8 +31,6 @@ class ConfigTranslationDeleteForm extends ConfirmFormBase { */ protected $language; - protected $mapper; - /** * {@inheritdoc} */ @@ -65,16 +64,10 @@ class ConfigTranslationDeleteForm extends ConfirmFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, array &$form_state, ConfigMapperInterface $mapper = NULL) { - // Get configuration group for this mapper. - /* - // @TODO: find a way to get Request object here. - $entity = $request->attributes->get($mapper->getType()); - $this->group = $mapper->getConfigGroup($entity); - $langcode = $request->attributes->get('langcode'); - $this->language = language_load($langcode); - */ - return parent::buildForm($form, $form_state); + public function buildForm(array $form, array &$form_state, ConfigMapperInterface $group = NULL, $language = NULL) { + $this->group = $group; + $this->language = $language; + return parent::buildForm($form, $form_state); } /** diff --git a/lib/Drupal/config_translation/Routing/RouteSubscriber.php b/lib/Drupal/config_translation/Routing/RouteSubscriber.php index 3c5d3bd..81fdd21 100644 --- a/lib/Drupal/config_translation/Routing/RouteSubscriber.php +++ b/lib/Drupal/config_translation/Routing/RouteSubscriber.php @@ -7,9 +7,9 @@ namespace Drupal\config_translation\Routing; use \Drupal\Core\Routing\RouteBuildEvent; use \Drupal\Core\Routing\RoutingEvents; -use Drupal\config_translation\Controller\ConfigTranslationController; use \Symfony\Component\EventDispatcher\EventSubscriberInterface; use \Symfony\Component\Routing\Route; + /** * Listens to the dynamic route events. */ @@ -38,7 +38,7 @@ class RouteSubscriber implements EventSubscriberInterface { */ public function routes(RouteBuildEvent $event) { $collection = $event->getRouteCollection(); - $config_groups = ConfigTranslationController::getTranslationGroups(); + $config_groups = config_translation_get_groups(); foreach ($config_groups as $group) { $path = $group->getBasePath(); $depth = substr_count($path, '/'); @@ -59,7 +59,7 @@ class RouteSubscriber implements EventSubscriberInterface { ),array( '_config_translation_config_name_access' => 'TRUE', )); - $collection->add($group->getRouterName() . '_add', $route); + $collection->add($group->getRouterName() . '.add', $route); $route = new Route($path . '/translate/edit/{langcode}', array( '_controller' => '\Drupal\config_translation\Controller\ConfigTranslationController::itemTranslatePage', @@ -68,15 +68,15 @@ class RouteSubscriber implements EventSubscriberInterface { ),array( '_config_translation_config_name_access' => 'TRUE', )); - $collection->add($group->getRouterName() . '_edit', $route); + $collection->add($group->getRouterName() . '.edit', $route); $route = new Route($path . '/translate/delete/{langcode}', array( - '_form' => '\Drupal\config_translation\Form\ConfigTranslationDeleteForm', + '_controller' => '\Drupal\config_translation\Controller\ConfigTranslationController::itemDeletePage', 'mapper' => $group, - ), array( - '_config_translation_config_name_access' => 'TRUE', - )); - $collection->add($group->getRouterName() . '_delete', $route); + ),array( + '_config_translation_config_name_access' => 'TRUE', + )); + $collection->add($group->getRouterName() . '.delete', $route); } } }