diff options
Diffstat (limited to 'modules/translation/translation.module')
-rw-r--r-- | modules/translation/translation.module | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/modules/translation/translation.module b/modules/translation/translation.module index 674492d98..c7f464cd2 100644 --- a/modules/translation/translation.module +++ b/modules/translation/translation.module @@ -336,3 +336,44 @@ function translation_node_get_translations($tnid) { function translation_supported_type($type) { return variable_get('language_' . $type, 0) == TRANSLATION_ENABLED; } + +/** + * Return paths of all translations of a node, based on + * its Drupal path. + * + * @param $path + * A Drupal path, for example node/432. + * @return + * An array of paths of translations of the node accessible + * to the current user keyed with language codes. + */ +function translation_path_get_translations($path) { + $paths = array(); + // Check for a node related path, and for its translations. + if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) { + foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) { + $paths[$language] = 'node/'. $translation_node->nid . $matches[2]; + } + } + return $paths; +} + +/** + * Implementation of hook_alter_translation_link(). + * + * Replaces links with pointers to translated versions of the content. + */ +function translation_translation_link_alter(&$links, $path) { + if ($paths = translation_path_get_translations($path)) { + foreach ($links as $langcode => $link) { + if (isset($paths[$langcode])) { + // Translation in a different node. + $links[$langcode]['href'] = $paths[$langcode]; + } + else { + // No translation in this language, or no permission to view. + unset($links[$langcode]); + } + } + } +} |