diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index a662259..d0ff900 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -277,7 +277,7 @@ public function getAccessController($entity_type) { * @return mixed. * A controller instance. */ - protected function getController($entity_type, $controller_type) { + public function getController($entity_type, $controller_type) { if (!isset($this->controllers[$controller_type][$entity_type])) { $class = $this->getControllerClass($entity_type, $controller_type); if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) { diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index 34e918c..0b0cfc4 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -149,9 +149,8 @@ function content_translation_menu() { $items["$path/translations"] = array( 'title' => 'Translate', - 'page callback' => 'content_translation_overview', - 'page arguments' => array($entity_position), 'type' => MENU_LOCAL_TASK, + 'route_name' => "content_translation.translation_overview.$entity_type", 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, 'weight' => 2, ) + $item; @@ -165,31 +164,22 @@ function content_translation_menu() { // Add translation callback. // @todo Add the access callback instead of replacing it as soon as the // routing system supports multiple callbacks. - $language_position = $entity_position + 3; - $args = array($entity_position, $language_position, $language_position + 1); $items["$path/translations/add/%language/%language"] = array( 'title' => 'Add', - 'page callback' => 'content_translation_add_page', - 'page arguments' => $args, - 'access callback' => 'content_translation_add_access', - 'access arguments' => $args, - 'type' => MENU_LOCAL_TASK, - 'weight' => 1, - ) + $item; + 'route_name' => "content_translation.translation_add.$entity_type", + 'weight' => 1, + ); // Edit translation callback. - $args = array($entity_position, $language_position); $items["$path/translations/edit/%language"] = array( 'title' => 'Edit', - 'page callback' => 'content_translation_edit_page', - 'page arguments' => $args, - 'access callback' => 'content_translation_edit_access', - 'access arguments' => $args, - 'type' => MENU_LOCAL_TASK, - 'weight' => 1, - ) + $item; + 'route_name' => "content_translation.translation_edit.$entity_type", + 'weight' => 1, + ); // Delete translation callback. + $language_position = $entity_position + 3; + $args = array($entity_position, $language_position, $language_position + 1); $items["$path/translations/delete/%language"] = array( 'title' => 'Delete', 'page callback' => 'drupal_get_form', @@ -298,42 +288,6 @@ function content_translation_view_access(EntityInterface $entity, $langcode, Acc } /** - * Access callback for the translation addition page. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being translated. - * @param \Drupal\Core\Language\Language $source - * (optional) The language of the values being translated. Defaults to the - * entity language. - * @param \Drupal\Core\Language\Language $target - * (optional) The language of the translated values. Defaults to the current - * content language. - */ -function content_translation_add_access(EntityInterface $entity, Language $source = NULL, Language $target = NULL) { - $source = !empty($source) ? $source : $entity->language(); - $target = !empty($target) ? $target : language(Language::TYPE_CONTENT); - $translations = $entity->getTranslationLanguages(); - $languages = language_list(); - return $source->id != $target->id && isset($languages[$source->id]) && isset($languages[$target->id]) && !isset($translations[$target->id]) && content_translation_access($entity, 'create'); -} - -/** - * Access callback for the translation edit page. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being translated. - * @param \Drupal\Core\Language\Language $language - * (optional) The language of the translated values. Defaults to the current - * content language. - */ -function content_translation_edit_access(EntityInterface $entity, Language $language = NULL) { - $language = !empty($language) ? $language : language(Language::TYPE_CONTENT); - $translations = $entity->getTranslationLanguages(); - $languages = language_list(); - return isset($languages[$language->id]) && $language->id != $entity->getUntranslated()->language()->id && isset($translations[$language->id]) && content_translation_access($entity, 'update'); -} - -/** * Access callback for the translation delete page. * * @param \Drupal\Core\Entity\EntityInterface $entity @@ -1043,3 +997,91 @@ function content_translation_save_settings($settings) { entity_info_cache_clear(); menu_router_rebuild(); } + +/** + * Returns the localized links for the given path. + * + * @param string $path + * The path for which language switch links should be provided. + * + * @returns + * A renderable array of language switch links. + */ +function _content_translation_get_switch_links($path) { + $links = language_negotiation_get_switch_links(Language::TYPE_CONTENT, $path); + if (empty($links)) { + // If content language is set up to fall back to the interface language, + // then there will be no switch links for Language::TYPE_CONTENT, ergo we + // also need to use interface switch links. + $links = language_negotiation_get_switch_links(Language::TYPE_INTERFACE, $path); + } + return $links; +} + +/** + * Populates target values with the source values. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entitiy being translated. + * @param \Drupal\Core\Language\Language $source + * The language to be used as source. + * @param \Drupal\Core\Language\Language $target + * The language to be used as target. + */ +function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) { + // @todo Unify field and property handling. + $entity = $entity->getNGEntity(); + if ($entity instanceof EntityNG) { + $source_translation = $entity->getTranslation($source->id); + $entity->addTranslation($target->id, $source_translation->getPropertyValues()); + } + else { + $instances = field_info_instances($entity->entityType(), $entity->bundle()); + foreach ($instances as $field_name => $instance) { + $field = field_info_field($field_name); + if (!empty($field['translatable'])) { + $value = $entity->get($field_name); + $value[$target->id] = isset($value[$source->id]) ? $value[$source->id] : array(); + $entity->set($field_name, $value); + } + } + } +} + +/** + * Form constructor for the translation deletion confirmation. + */ +function content_translation_delete_confirm(array $form, array $form_state, EntityInterface $entity, Language $language) { + $langcode = $language->id; + $controller = content_translation_controller($entity->entityType()); + + return confirm_form( + $form, + t('Are you sure you want to delete the @language translation of %label?', array('@language' => $language->name, '%label' => $entity->label())), + $controller->getEditPath($entity), + t('This action cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +/** + * Form submission handler for content_translation_delete_confirm(). + */ +function content_translation_delete_confirm_submit(array $form, array &$form_state) { + list($entity, $language) = $form_state['build_info']['args']; + $controller = content_translation_controller($entity->entityType()); + + // Remove the translated values. + $controller->removeTranslation($entity, $language->id); + $entity->save(); + + // Remove any existing path alias for the removed translation. + // @todo This should be taken care of by the Path module. + if (module_exists('path')) { + $conditions = array('source' => $controller->getViewPath($entity), 'langcode' => $language->id); + Drupal::service('path.crud')->delete($conditions); + } + + $form_state['redirect'] = $controller->getBasePath($entity) . '/translations'; +} diff --git a/core/modules/content_translation/content_translation.pages.inc b/core/modules/content_translation/content_translation.pages.inc deleted file mode 100644 index e277791..0000000 --- a/core/modules/content_translation/content_translation.pages.inc +++ /dev/null @@ -1,295 +0,0 @@ -entityType()); - $entity_manager = Drupal::entityManager(); - $languages = language_list(); - $original = $entity->getUntranslated()->language()->id; - $translations = $entity->getTranslationLanguages(); - $field_ui = module_exists('field_ui') && user_access('administer ' . $entity->entityType() . ' fields'); - - $path = $controller->getViewPath($entity); - $base_path = $controller->getBasePath($entity); - $edit_path = $controller->getEditPath($entity); - - $header = array(t('Language'), t('Translation'), t('Source language'), t('Status'), t('Operations')); - $rows = array(); - - if (language_multilingual()) { - // If we have a view path defined for the current entity get the switch - // links based on it. - if ($path) { - $links = _content_translation_get_switch_links($path); - } - - // Determine whether the current entity is translatable. - $translatable = FALSE; - foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; - $field = field_info_field($field_name); - if ($field['translatable']) { - $translatable = TRUE; - break; - } - } - - foreach ($languages as $language) { - $language_name = $language->name; - $langcode = $language->id; - $add_path = $base_path . '/translations/add/' . $original . '/' . $langcode; - $translate_path = $base_path . '/translations/edit/' . $langcode; - $delete_path = $base_path . '/translations/delete/' . $langcode; - - if ($base_path) { - $add_links = _content_translation_get_switch_links($add_path); - $edit_links = _content_translation_get_switch_links($edit_path); - $translate_links = _content_translation_get_switch_links($translate_path); - $delete_links = _content_translation_get_switch_links($delete_path); - } - - $operations = array( - 'data' => array( - '#type' => 'operations', - '#links' => array(), - ), - ); - $links = &$operations['data']['#links']; - - if (isset($translations[$langcode])) { - // Existing translation in the translation set: display status. - $source = isset($entity->translation[$langcode]['source']) ? $entity->translation[$langcode]['source'] : ''; - $is_original = $langcode == $original; - $translation = $translations[$langcode]; - $label = $entity->label($langcode); - $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array('href' => $path, 'language' => $language); - $row_title = l($label, $link['href'], $link); - - if (empty($link['href'])) { - $row_title = $is_original ? $label : t('n/a'); - } - - // If the user is allowed to edit the entity we point the edit link to - // the entity form, otherwise if we are not dealing with the original - // language we point the link to the translation form. - if ($edit_path && $entity->access('update')) { - $links['edit'] = isset($edit_links->links[$langcode]['href']) ? $edit_links->links[$langcode] : array('href' => $edit_path, 'language' => $language); - } - elseif (!$is_original && $controller->getTranslationAccess($entity, 'update')) { - $links['edit'] = isset($translate_links->links[$langcode]['href']) ? $translate_links->links[$langcode] : array('href' => $translate_path, 'language' => $language); - } - - if (isset($links['edit'])) { - $links['edit']['title'] = t('Edit'); - } - - $translation = $entity->translation[$langcode]; - $status = !empty($translation['status']) ? t('Published') : t('Not published'); - // @todo Add a theming function here. - $status = '' . $status . '' . (!empty($translation['outdated']) ? ' ' . t('outdated') . '' : ''); - - if ($is_original) { - $language_name = t('@language_name (Original language)', array('@language_name' => $language_name)); - $source_name = t('n/a'); - } - else { - $source_name = isset($languages[$source]) ? $languages[$source]->name : t('n/a'); - if ($controller->getTranslationAccess($entity, 'delete')) { - $links['delete'] = isset($delete_links->links[$langcode]['href']) ? $delete_links->links[$langcode] : array('href' => $delete_links, 'language' => $language); - $links['delete']['title'] = t('Delete'); - } - } - } - else { - // No such translation in the set yet: help user to create it. - $row_title = $source_name = t('n/a'); - $source = $entity->language()->id; - - if ($source != $langcode && $controller->getTranslationAccess($entity, 'create')) { - if ($translatable) { - $links['add'] = isset($add_links->links[$langcode]['href']) ? $add_links->links[$langcode] : array('href' => $add_path, 'language' => $language); - $links['add']['title'] = t('Add'); - } - elseif ($field_ui) { - $entity_path = $entity_manager->getAdminPath($entity->entityType(), $entity->bundle()); - // Link directly to the fields tab to make it easier to find the - // setting to enable translation on fields. - $path = $entity_path . '/fields'; - $links['nofields'] = array('title' => t('No translatable fields'), 'href' => $path, 'language' => $language); - } - } - - $status = t('Not translated'); - } - - $rows[] = array($language_name, $row_title, $source_name, $status, $operations); - } - } - - drupal_set_title(t('Translations of %label', array('%label' => $entity->label())), PASS_THROUGH); - - // Add metadata to the build render array to let other modules know about - // which entity this is. - $build['#entity'] = $entity; - - $build['content_translation_overview'] = array( - '#theme' => 'table', - '#header' => $header, - '#rows' => $rows, - ); - - return $build; -} - -/** - * Returns the localized links for the given path. - * - * @param string $path - * The path for which language switch links should be provided. - * - * @returns - * A renderable array of language switch links. - */ -function _content_translation_get_switch_links($path) { - $links = language_negotiation_get_switch_links(Language::TYPE_CONTENT, $path); - if (empty($links)) { - // If content language is set up to fall back to the interface language, - // then there will be no switch links for Language::TYPE_CONTENT, ergo we - // also need to use interface switch links. - $links = language_negotiation_get_switch_links(Language::TYPE_INTERFACE, $path); - } - return $links; -} - -/** - * Page callback for the translation addition page. - * - * @param EntityInterface $entity - * The entity being translated. - * @param Language $source - * (optional) The language of the values being translated. Defaults to the - * entity language. - * @param Language $target - * (optional) The language of the translated values. Defaults to the current - * content language. - * - * @return array - * A processed form array ready to be rendered. - */ -function content_translation_add_page(EntityInterface $entity, Language $source = NULL, Language $target = NULL) { - $source = !empty($source) ? $source : $entity->language(); - $target = !empty($target) ? $target : language(Language::TYPE_CONTENT); - // @todo Exploit the upcoming hook_entity_prepare() when available. - content_translation_prepare_translation($entity, $source, $target); - $info = $entity->entityInfo(); - $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default'; - $form_state['langcode'] = $target->id; - $form_state['content_translation']['source'] = $source; - $form_state['content_translation']['target'] = $target; - $controller = content_translation_controller($entity->entityType()); - $form_state['content_translation']['translation_form'] = !$entity->access('update'); - return Drupal::entityManager()->getForm($entity, $operation, $form_state); -} - -/** - * Page callback for the translation edit page. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being translated. - * @param \Drupal\Core\Language\Language $language - * (optional) The language of the translated values. Defaults to the current - * content language. - * - * @return array - * A processed form array ready to be rendered. - */ -function content_translation_edit_page(EntityInterface $entity, Language $language = NULL) { - $language = !empty($language) ? $language : language(Language::TYPE_CONTENT); - $info = $entity->entityInfo(); - $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default'; - $form_state['langcode'] = $language->id; - $form_state['content_translation']['translation_form'] = TRUE; - return Drupal::entityManager()->getForm($entity, $operation, $form_state); -} - -/** - * Populates target values with the source values. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entitiy being translated. - * @param \Drupal\Core\Language\Language $source - * The language to be used as source. - * @param \Drupal\Core\Language\Language $target - * The language to be used as target. - */ -function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) { - // @todo Unify field and property handling. - $entity = $entity->getNGEntity(); - if ($entity instanceof EntityNG) { - $source_translation = $entity->getTranslation($source->id); - $entity->addTranslation($target->id, $source_translation->getPropertyValues()); - } - else { - $instances = field_info_instances($entity->entityType(), $entity->bundle()); - foreach ($instances as $field_name => $instance) { - $field = field_info_field($field_name); - if (!empty($field['translatable'])) { - $value = $entity->get($field_name); - $value[$target->id] = isset($value[$source->id]) ? $value[$source->id] : array(); - $entity->set($field_name, $value); - } - } - } -} - -/** - * Form constructor for the translation deletion confirmation. - */ -function content_translation_delete_confirm(array $form, array $form_state, EntityInterface $entity, Language $language) { - $langcode = $language->id; - $controller = content_translation_controller($entity->entityType()); - - return confirm_form( - $form, - t('Are you sure you want to delete the @language translation of %label?', array('@language' => $language->name, '%label' => $entity->label())), - $controller->getEditPath($entity), - t('This action cannot be undone.'), - t('Delete'), - t('Cancel') - ); -} - -/** - * Form submission handler for content_translation_delete_confirm(). - */ -function content_translation_delete_confirm_submit(array $form, array &$form_state) { - list($entity, $language) = $form_state['build_info']['args']; - $controller = content_translation_controller($entity->entityType()); - - // Remove the translated values. - $controller->removeTranslation($entity, $language->id); - $entity->save(); - - // Remove any existing path alias for the removed translation. - // @todo This should be taken care of by the Path module. - if (module_exists('path')) { - $conditions = array('source' => $controller->getViewPath($entity), 'langcode' => $language->id); - Drupal::service('path.crud')->delete($conditions); - } - - $form_state['redirect'] = $controller->getBasePath($entity) . '/translations'; -} diff --git a/core/modules/content_translation/content_translation.services.yml b/core/modules/content_translation/content_translation.services.yml index 1a4a939..44272d5 100644 --- a/core/modules/content_translation/content_translation.services.yml +++ b/core/modules/content_translation/content_translation.services.yml @@ -2,3 +2,18 @@ services: content_translation.synchronizer: class: Drupal\content_translation\FieldTranslationSynchronizer arguments: ['@plugin.manager.entity'] + content_translation.subscriber: + class: Drupal\content_translation\Routing\ContentTranslationRouteSubscriber + arguments: ['@plugin.manager.entity'] + tags: + - { name: event_subscriber } + content_translation.overview_access: + class: Drupal\content_translation\Access\ContentTranslationOverviewAccess + arguments: ['@plugin.manager.entity'] + tags: + - { name: access_check } + content_translation.manage_access: + class: Drupal\content_translation\Access\ContentTranslationManageAccessCheck + arguments: ['@plugin.manager.entity'] + tags: + - { name: access_check } 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 new file mode 100644 index 0000000..991ff01 --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationManageAccessCheck.php @@ -0,0 +1,88 @@ +entityManager = $manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity') + ); + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return array_key_exists('_access_content_translation_manage', $route->getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + if ($entity = $request->attributes->get('entity')) { + $route_requirements = $route->getRequirements(); + $operation = $route_requirements['_access_content_translation_manage']; + $controller = $this->entityManager->getController($entity->entityType(), $entity); + + // Load translation. + $translations = $entity->getTranslationLanguages(); + $languages = language_list(); + + switch ($operation) { + case 'create': + $source = language_load($request->attributes->get('source')); + $target = language_load($request->attributes->get('target')); + return $source->langcode != $target->langcode && isset($languages[$source->langcode]) + && isset($languages[$target->langcode]) + && !isset($translations[$target->langcode]) + && $controller->getTranslationAccess($entity, $operation); + break; + case 'update': + $language = language_load($request->attributes->get('language')); + return isset($languages[$language->langcode]) + && $language->langcode != $entity->language()->langcode + && isset($translations[$language->langcode]) + && $controller->getTranslationAccess($entity, $operation); + break; + } + } + return static::DENY; + } +} 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 new file mode 100644 index 0000000..a8b105c --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Access/ContentTranslationOverviewAccess.php @@ -0,0 +1,86 @@ +entityManager = $manager; + } + + /** + * {inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity') + ); + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return array_key_exists('_content_translation_access', $route->getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + if ($entity = $request->attributes->get('entity')) { + // Get entity base info. + $entity_type = $entity->entityType(); + $bundle = $entity->bundle(); + + // Get account details from request. + $account = $request->attributes->get('_account'); + + // Get entity access callback. + $definitions = $this->entityManager->getDefinitions(); + $access_callback = $definitions[$entity_type]['translation']['content_translation']['access_callback']; + if (call_user_func($access_callback, $entity)) { + return static::ALLOW; + } + + // Check per entity permission. + $permission = "translate {$entity_type}"; + if ($definitions[$entity_type]['permission_granularity'] == 'bundle') { + $permission = "translate {$bundle} {$entity_type}"; + } + if ($account->hasPermission($permission)) { + return static::ALLOW; + } + } + + return static::DENY; + } +} 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 new file mode 100644 index 0000000..27ae12d --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Controller/ContentTranslationController.php @@ -0,0 +1,312 @@ +entityManager = $entity_manager; + $this->moduleHandler = $module_handler; + $this->fieldInfo = $field_info; + $this->translator = $translator; + } + + /** + * {inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity'), + $container->get('module_handler'), + $container->get('field.info'), + $container->get('string_translation'), + ); + } + + /** + * Translations overview page builder. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to be translated. + * @param \Drupal\Core\Session\AccountInterface $account + * The account to check, if not given use currently logged in user. + * + * @return array + * Array of page elements to render. + */ + public function overview(EntityInterface $entity, AccountInterface $account = NULL) { + $entity_type = $entity->entityType(); + + $controller = $this->entityManager->getController($entity_type, 'translation'); + + $languages = language_list(); + $original = $entity->language()->langcode; + $translations = $entity->getTranslationLanguages(); + $field_ui = $this->moduleHandler->moduleExists('field_ui') && $account->hasPermission('administer ' . $entity_type . ' fields'); + + $path = $controller->getViewPath($entity); + $base_path = $controller->getBasePath($entity); + $edit_path = $controller->getEditPath($entity); + + $header = array( + $this->translator->translate('Language'), + $this->translator->translate('Translation'), + $this->translator->translate('Source language'), + $this->translator->translate('Status'), + $this->translator->translate('Operations'), + ); + $rows = array(); + + if (language_multilingual()) { + // If we have a view path defined for the current entity get the switch + // links based on it. + if ($path) { + $links = _content_translation_get_switch_links($path); + } + + // Determine whether the current entity is translatable. + $translatable = FALSE; + foreach ($this->fieldInfo->getInstances($entity_type) as $instance) { + $field_name = $instance['field_name']; + $field = $this->fieldInfo->getField($field_name); + if ($field['translatable']) { + $translatable = TRUE; + break; + } + } + + foreach ($languages as $language) { + $language_name = $language->name; + $langcode = $language->langcode; + $add_path = $base_path . '/translations/add/' . $original . '/' . $langcode; + $translate_path = $base_path . '/translations/edit/' . $langcode; + $delete_path = $base_path . '/translations/delete/' . $langcode; + + if ($base_path) { + $add_links = _content_translation_get_switch_links($add_path); + $edit_links = _content_translation_get_switch_links($edit_path); + $translate_links = _content_translation_get_switch_links($translate_path); + $delete_links = _content_translation_get_switch_links($delete_path); + } + + $operations = array( + 'data' => array( + '#type' => 'operations', + '#links' => array(), + ), + ); + $links = &$operations['data']['#links']; + + if (isset($translations[$langcode])) { + // Existing translation in the translation set: display status. + $source = isset($entity->translation[$langcode]['source']) ? $entity->translation[$langcode]['source'] : ''; + $is_original = $langcode == $original; + $translation = $translations[$langcode]; + $label = $entity->label($langcode); + $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array( + 'href' => $path, + 'language' => $language, + ); + $row_title = l($label, $link['href'], $link); + + if (empty($link['href'])) { + $row_title = $is_original ? $label : $this->translator->translate('n/a'); + } + + // If the user is allowed to edit the entity we point the edit link to + // the entity form, otherwise if we are not dealing with the original + // language we point the link to the translation form. + if ($edit_path && $controller->getAccess($entity, 'update')) { + $links['edit'] = isset($edit_links->links[$langcode]['href']) ? $edit_links->links[$langcode] : array( + 'href' => $edit_path, + 'language' => $language, + ); + } + elseif (!$is_original && $controller->getTranslationAccess($entity, 'update')) { + $links['edit'] = isset($translate_links->links[$langcode]['href']) ? $translate_links->links[$langcode] : array( + 'href' => $translate_path, + 'language' => $language, + ); + } + + if (isset($links['edit'])) { + $links['edit']['title'] = $this->translator->translate('Edit'); + } + $translation = $entity->translation[$langcode]; + $status = !empty($translation['status']) ? $this->translator->translate('Published') : $this->translator->translate('Not published'); + // @todo Add a theming function here. + $status = '' . $status . '' . (!empty($translation['outdated']) ? ' ' . $this->translator->translate('outdated') . '' : ''); + + if ($is_original) { + $language_name = $this->translator->translate('@language_name (Original language)', array('@language_name' => $language_name)); + $source_name = $this->translator->translate('n/a'); + } + else { + $source_name = isset($languages[$source]) ? $languages[$source]->name : $this->translator->translate('n/a'); + if ($controller->getTranslationAccess($entity, 'delete')) { + $links['delete'] = isset($delete_links->links[$langcode]['href']) ? $delete_links->links[$langcode] : array( + 'href' => $delete_links, + 'language' => $language, + ); + $links['delete']['title'] = $this->translator->translate('Delete'); + } + } + } + else { + // No such translation in the set yet: help user to create it. + $row_title = $source_name = $this->translator->translate('n/a'); + $source = $entity->language()->langcode; + + if ($source != $langcode && $controller->getTranslationAccess($entity, 'create')) { + if ($translatable) { + $links['add'] = isset($add_links->links[$langcode]['href']) ? $add_links->links[$langcode] : array( + 'href' => $add_path, + 'language' => $language, + ); + $links['add']['title'] = $this->translator->translate('Add'); + } + elseif ($field_ui) { + $entity_path = $this->entityManager->getAdminPath($entity_type, $entity->bundle()); + // Link directly to the fields tab to make it easier to find the + // setting to enable translation on fields. + $path = $entity_path . '/fields'; + $links['nofields'] = array( + 'title' => $this->translator->translate('No translatable fields'), + 'href' => $path, + 'language' => $language, + ); + } + } + + $status = $this->translator->translate('Not translated'); + } + + $rows[] = array( + $language_name, + $row_title, + $source_name, + $status, + $operations, + ); + } + } + + drupal_set_title($this->translator->translate('Translations of %label', array('%label' => $entity->label())), PASS_THROUGH); + + // Add metadata to the build render array to let other modules know about + // which entity this is. + $build['#entity'] = $entity; + + $build['content_translation_overview'] = array( + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + ); + + return $build; + } + + /** + * Translation addition page builder. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity being translated. + * @param \Drupal\language\Plugin\Condition\Language $source + * (optional) The language of the values being translated. Defaults to the + * entity language. + * @param \Drupal\language\Plugin\Condition\Language $target + * (optional) The language of the translated values. Defaults to the current + * content language. + * + * @return array + * A processed form array ready to be rendered. + */ + public function add(EntityInterface $entity, $source = NULL, $target = NULL) { + $source = language_load($source); + $target = language_load($target); + // @todo Exploit the upcoming hook_entity_prepare() when available. + content_translation_prepare_translation($entity, $source, $target); + $info = $entity->entityInfo(); + $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default'; + $form_state['langcode'] = $target->langcode; + $form_state['content_translation']['source'] = $source; + $form_state['content_translation']['target'] = $target; + $controller = content_translation_controller($entity->entityType()); + $form_state['content_translation']['translation_form'] = !$controller->getAccess($entity, 'update'); + $this->entityManager->getForm($entity, $operation, $form_state); + } + + /** + * Page callback for the edit translation page. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity being translated. + * @param \Drupal\language\Plugin\Condition\Language $language + * (optional) The language of the translated values. Defaults to the current + * content language. + * + * @return array + * A processed form array ready to be rendered. + */ + public function edit(EntityInterface $entity, $language = NULL) { + $language = language_load($language); + $info = $entity->entityInfo(); + $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default'; + $form_state['langcode'] = $language->langcode; + $form_state['content_translation']['translation_form'] = TRUE; + $this->entityManager->getForm($entity, $operation, $form_state); + } +} 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 new file mode 100644 index 0000000..8e7bbf2 --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php @@ -0,0 +1,108 @@ +entityManager = $entityManager; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[RoutingEvents::DYNAMIC] = 'routes'; + return $events; + } + + /** + * Adds routes for entity translations. + */ + 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'])) { + $route = new Route( + '/' . str_replace($entity_info['menu_path_wildcard'], '{entity}', $entity_info['menu_base_path']) . "/translations", + array( + '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::overview', + ), + array( + '_content_translation_access' => $entity_type, + '_permission' => 'translate any entity', + ), + array( + 'converters' => array( + 'entity' => $entity_type, + ), + ) + ); + $collection->add("content_translation.translation_overview.$entity_type", $route); + + $route = new Route( + '/' . str_replace($entity_info['menu_path_wildcard'], '{entity}', $entity_info['menu_base_path']) . "/translations/add/{source}/{target}", + array( + '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::add', + + ), + array( + '_permission' => 'translate any entity', + '_access_content_translation_manage' => 'create', + ), + array( + 'converters' => array( + 'entity' => $entity_type, + ), + ) + ); + $collection->add("content_translation.translation_add.$entity_type", $route); + + $route = new Route( + '/' . str_replace($entity_info['menu_path_wildcard'], '{entity}', $entity_info['menu_base_path']) . "/translations/edit/{language}", + array( + '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::edit', + + ), + array( + '_permission' => 'translate any entity', + '_access_content_translation_manage' => 'update', + ), + array( + 'converters' => array( + 'entity' => $entity_type, + ), + ) + ); + $collection->add("content_translation.translation_edit.$entity_type", $route); + } + } + } +}