'Node title autocomplete', 'page callback' => 'i18n_node_autocomplete', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, 'file' => 'i18n_node.pages.inc', ); return $items; } */ /** * Replacement for node_add * * This just calls node_add() and switches title. This has to be done here to work always */ function i18n_node_add($type) { global $user; $types = node_type_get_types(); $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => LANGUAGE_NONE); drupal_set_title(t('Create @name', array('@name' => i18n_node_translate_type($types[$type], 'name'))), PASS_THROUGH); $output = drupal_get_form($type . '_node_form', $node); return $output; } /** * Replacement for node_add_page. */ function i18n_node_add_page() { $item = menu_get_item(); $content = system_admin_menu_block($item); // Bypass the node/add listing if only one content type is available. if (count($content) == 1) { $item = array_shift($content); drupal_goto($item['href']); } foreach ($content as &$item) { // Type machine name will be the third part of the path $parts = explode('/', $item['path']); $type = $parts[2]; // We just need to translate the description, the title is translated by the menu system // The string will be filtered (xss_admin) on the theme layer $item['description'] = i18n_node_translate_type($type, 'description', $item['description']); } return theme('node_add_list', array('content' => $content)); } /** * Implements hook_field_extra_fields(). */ function i18n_node_field_extra_fields() { $return = array(); $info = entity_get_info('node'); foreach (array_keys($info['bundles']) as $bundle) { if (i18n_node_type_enabled($bundle)) { $return['node'][$bundle] = i18n_language_field_extra(); } } return $return; } /** * Implements hook_menu(). */ function i18n_node_menu() { $items['admin/config/regional/i18n/node'] = array( 'title' => 'Node options', 'description' => 'Configure extended options for multilingual content and translations.', 'page callback' => 'drupal_get_form', 'page arguments' => array('variable_module_form', 'i18n_node'), 'access arguments' => array('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, ); return $items; } /** * Implements hook_block_view_MODULE_DELTA_alter(). * * Set translated help text for node/add pages, replace node_help() text. */ function i18n_node_block_view_system_help_alter(&$block) { $arg = drupal_help_arg(arg(NULL)); if (module_exists('i18n_string') && $arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) { if (($type = node_type_get_type(str_replace('-', '_', $arg[2]))) && !empty($type->help)) { $help = i18n_node_translate_type($type, 'help'); if ($help !== $type->help) { $block['content'] = str_replace(filter_xss_admin($type->help), filter_xss_admin($help), $block['content']); } } } } /** * Implements hook_help(). */ function i18n_node_help($path, $arg) { switch ($path) { case 'admin/help#i18n_node': $output = '
' . t('Provides some extended multilingual options for nodes.') . '
'; $output .= ''. t('Aditionally, if String translation enabled, this module will localize all content type configuration texts.') .'
'; $output .= ''. t('To search and translate strings, use the translation interface pages.', array('@translate-interface' => url('admin/build/translate'))) .'
'; return $output; } } /** * Implements hook_i18n_context_language() */ function i18n_node_i18n_context_language() { // Node language when loading specific nodes or creating translations. if (arg(0) == 'node' ) { if (($node = menu_get_object('node')) && !empty($node->language)) { return i18n_language($node->language); } elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['target'])) { return i18n_language($_GET['target']); } } } /** * Implements hook_i18n_string_info() */ function i18n_node_i18n_string_info() { $groups['node'] = array( 'title' => t('Node types'), 'description' => t('Content type names, descriptions, help texts.'), //'format' => TRUE, // This group has strings with format (block body) 'list' => TRUE, // This group can list all strings ); return $groups; } /** * Implements hook_i18n_string_list() */ function i18n_node_i18n_string_list($group) { if ($group == 'node' || $group = 'all') { $strings = array(); foreach (node_type_get_types() as $type) { $strings['node']['type'][$type->type] = array( 'name' => $type->name, 'title_label' => $type->title_label, 'description' => $type->description, 'help' => $type->help, ); } return $strings; } } /** * Implements hook i18n_object_info() */ function i18n_node_i18n_object_info() { $info['node_type'] = array( 'title' => t('Node type'), 'key' => 'type', 'string translation' => array( 'textgroup' => 'node', 'type' => 'type', 'properties' => array( 'name' => t('Name'), 'title_label' => t('Title label'), 'description' => t('Description'), 'help' => t('Help text'), ), ) ); return $info; } /** * Implements hook_i18n_translate_path() */ function i18n_node_i18n_translate_path($path, $language) { if (preg_match("!^node/(\d+)(/.+|)!", $path, $matches) && ($node = node_load((int) $matches[1])) && i18n_object_langcode($node) && !empty($node->tnid)) { $translations = translation_node_get_translations($node->tnid); if (!empty($translations[$language->language])) { return 'node/' . $translations[$language->language]->nid . $matches[2]; } } } /** * Implements hook_menu_alter(). * * Take over the node translation page. */ function i18n_node_menu_alter(&$items) { if (isset($items['node/%node/translate'])) { $items['node/%node/translate']['page callback'] = 'i18n_node_translation_overview'; $items['node/%node/translate']['file'] = 'i18n_node.pages.inc'; $items['node/%node/translate']['module'] = 'i18n_node'; } // Take over node/add pages for string translation if (module_exists('i18n_string')) { $items['node/add']['page callback'] = 'i18n_node_add_page'; // @TODO avoid iterating over every router path. foreach (node_type_get_types() as $type) { $path = 'node/add/' . str_replace('_', '-', $type->type); if (isset($items[$path])) { if (!empty($items[$path]['page callback']) && $items[$path]['page callback'] == 'node_add') { $items[$path]['page callback'] = 'i18n_node_add'; } $items[$path]['title callback'] = 'i18n_node_type_name'; $items[$path]['title arguments'] = array($type->type, $type->name); } } } } /** * Get node language. */ function i18n_node_get_lang($nid, $default = '') { $lang = db_result(db_query('SELECT language FROM {node} WHERE nid = %d', $nid)); return $lang ? $lang : $default ; } /** * Get allowed languages for node. * * This allows node types to define its own language list implementing hook 'language_list'. * * @param $node * Node to retrieve language list for. * @param $translate * Only languages available for translation. Filter out existing translations. */ function i18n_node_language_list($node, $translate = FALSE) { // Check if the node module manages its own language list. $languages = node_invoke($node, 'language_list', $translate); if (!$languages) { $languages = i18n_language_list('name', i18n_node_language_mode($node)); if ($translate && isset($node->tnid) && $node->tnid && ($translations = translation_node_get_translations($node->tnid))) { unset($translations[$node->language]); foreach (array_keys($translations) as $langcode) { unset($languages[$langcode]); } } // Language may be locked for this node type, restrict options to current one if (variable_get('i18n_lock_node_' . $node->type, 0) && !empty($node->language) && !empty($languages[$node->language])) { $languages = array($node->language => $languages[$node->language]); } // Check language required for this type (no language neutral) elseif (!variable_get('i18n_required_node_' . $node->type, 0)) { $languages = array(LANGUAGE_NONE => t('Language neutral')) + $languages; } } return $languages; } /** * Get language mode for node or node type */ function i18n_node_language_mode($type) { $type = is_object($type) ? $type->type : $type; return variable_get('i18n_node_'. $type, I18N_LANGUAGE_ENABLED); } /** * Implements hook_node_prepare(). */ function i18n_node_node_prepare($node) { global $language; if (i18n_node_type_enabled($node) && empty($node->nid) && (empty($node->language) OR $node->language == 'und') && variable_get('i18n_newnode_current_' . $node->type, 0)) { // Set current language for new nodes if option enabled $node->language = $language->language; } } /** * Implements hook_permission(). * * Permissions defined * - administer all languages * Disables language conditions for administration pages, so the user can view objects for all languages at the same time. * This applies for: menu items, taxonomy * - administer translations * Will allow to add/remove existing nodes to/from translation sets. */ function i18n_node_permission() { return array( 'administer content translations' => array( 'title' => t('Administer content translations'), 'description' => t('Add or remove existing content to translation sets.'), ), ); } /** * Implements hook_alter_translation_link(). * * Handles links for extended language. The links will have current language. * * @todo Update D7 */ function i18n_node_translation_link_alter(&$links, $path) { global $language; // 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)) { // make sure language support is set to I18N_LANGUAGE_EXTENDED, so links // dont get added for I18N_LANGUAGE_EXTENDED_NOT_DISPLAYED if (!(i18n_node_languabe_mode($node->type) & I18N_LANGUAGE_HIDDEN)) { $languages = language_list(); $extended = array(); foreach (translation_node_get_translations($node->tnid) as $langcode => $translation_node) { if (!isset($links[$langcode]) && isset($languages[$langcode])) { $extended[$langcode] = array( 'href' => 'node/'. $translation_node->nid . $matches[2], 'language' => $language, 'language_icon' => $languages[$langcode], 'title' => $languages[$langcode]->native, 'attributes' => array('class' => 'language-link'), ); } } // This will run after languageicon module, so we add icon in case that one is enabled. if ($extended && function_exists('languageicons_translation_link_alter')) { languageicons_translation_link_alter($extended, $path); } $links = array_merge($links, $extended); } } } /** * Implements hook_node_view() */ function i18n_node_node_view($node) { if (i18n_node_type_enabled($node)) { $node->content['language'] = array( '#type' => 'item', '#title' => t('Language'), '#markup' => i18n_language_name($node->language), ); } } /** * Implements hook_node_view_alter(). * * Handles links for extended languages. Sets current interface language. */ function i18n_node_node_view_alter(&$build) { global $language; $node = $build['#node']; $language_support = i18n_node_language_mode($node); // Hide node translation links. if (variable_get('i18n_hide_translation_links', 0)) { if (isset($build['links']['translation'])) { unset($build['links']['translation']); } } elseif (!empty($node->tnid) && !empty($build['links']['translation'])) { // Set the right languages depending on language mode (extended) $links = $build['links']['translation']['#links']; foreach (i18n_node_language_list($node) as $langcode => $langname) { $index = 'translation_'. $langcode; if ($langcode != $node->language && !empty($links[$index])) { if (!($language_support & I18N_LANGUAGE_EXTENDED) && $links[$index]['language']->enabled == 0) { // Remove disabled language if not language extended unset($build['links']['translation']['#links'][$index]); } else { $build['links']['translation']['#links'][$index]['language'] = $language; } } } } } /** * Implements hook_node_type_insert() */ function i18n_node_node_type_insert($info) { i18n_node_node_type_update($info); } /** * Implements hook_node_type_update() */ function i18n_node_node_type_update($info) { if (module_exists('i18n_string')) { if(!empty($info->old_type) && $info->old_type != $info->type) { i18n_string_update_context("node:type:$info->old_type:*", "node:type:$info->type:*"); } i18n_string_object_update('node_type', $info); } } /** * Implements hook_node_type_delete() */ function i18n_node_node_type_delete($info) { i18n_string_object_remove('node_type', $info); } /** * Implements hook_elements(). * * Add a process callback for textfields. * * * @todo Update D7 */ /* function i18n_node_elements() { $type = array(); $type['textfield'] = array('#process' => array('i18n_node_textfield_process')); return $type; } */ /** * Process callback for textfield elements. * * When editing or translating a node, set Javascript to rewrite autocomplete * paths to use the node language prefix rather than the current content one. * * @todo Update D7 */ function i18n_node_textfield_process($element) { global $language; static $sent = FALSE; // Ensure we send the Javascript only once. if (!$sent && isset($element['#autocomplete_path']) && !empty($element['#autocomplete_path']) && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) { // Add a JS file for node forms. // Determine if we are either editing or translating an existing node. // We can't act on regular node creation because we don't have a specified // node language. $node_edit = $node = menu_get_object() && arg(2) == 'edit' && isset($node->language) && !empty($node->language); $node_translate = arg(0) == 'node' && arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language']); if ($node_edit || $node_translate) { $node_language = $node_edit ? $node->language : $_GET['language']; // Only needed if the node language is different from the interface one. if ($node_language != $language->language) { $languages = language_list(); if (isset($languages[$node_language])) { drupal_add_js(drupal_get_path('module', 'i18n_node') . '/i18n_node.js'); // Pass the interface and content language base paths. // Remove any trailing forward slash. Doing so prevents a mismatch // that occurs when a language has no prefix and hence gets a path // with a trailing forward slash. $interface = rtrim(url('', array('absolute' => TRUE)), '/'); $content = rtrim(url('', array('absolute' => TRUE, 'language' => $languages[$node_language])), '/'); $data = array('interface_path' => $interface, 'content_path' => $content); drupal_add_js(array('i18n' => $data), 'setting'); } } } $sent = TRUE; } return $element; } /** * Implements hook_form_FORM_ID_alter(). */ function i18n_node_form_search_form_alter(&$form, &$form_state) { // Advanced search form. Add language and localize content type names if ($form['module']['#value'] == 'node' && !empty($form['advanced'])){ // @todo Handle language search conditions //$form['advanced']['language'] = _i18n_language_select(); if (!empty($form['advanced']['type'])) { foreach ($form['advanced']['type']['#options'] as $type => $name) { $form['advanced']['type']['#options'][$type] = i18n_node_type_translate($type, 'name', $name); } } } } /** * Implements hook_form_FORM_ID_alter(). */ function i18n_node_form_node_type_form_alter(&$form, &$form_state) { if (isset($form['type'])) { $disabled = !i18n_node_type_enabled($form['#node_type']); $form['i18n'] = array( '#type' => 'fieldset', '#title' => t('Multilingual settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#group' => 'additional_settings', '#attributes' => array( 'class' => array('i18n-node-type-settings-form'), ), '#description' => t('Extended multilingual options provided by Internationalization module.'), '#disabled' => $disabled, ); // Add disabled message if ($disabled) { $form['i18n']['#description'] .= ' ' . t('These will be available only when you enable Multilingual support in Workflow settings above.') . ''; } // Some settings about node languages $form['i18n']['options'] = array( '#title' => t('Options for node language'), '#type' => 'fieldset', '#disabled' => $disabled, ); $form['i18n']['options']['i18n_newnode_current'] = array( '#type' => 'checkbox', '#title' => t('Set current language as default for new content.'), '#default_value' => variable_get('i18n_newnode_current_' . $form['#node_type']->type, 0), '#disabled' => $disabled, ); $form['i18n']['options']['i18n_required_node'] = array( '#type' => 'checkbox', '#title' => t('Require language (Do not allow Language Neutral).'), '#default_value' => variable_get('i18n_required_node_' . $form['#node_type']->type, 0), '#disabled' => $disabled, ); $form['i18n']['options']['i18n_lock_node'] = array( '#type' => 'checkbox', '#title' => t('Lock language (Cannot be changed).'), '#default_value' => variable_get('i18n_lock_node_' . $form['#node_type']->type, 0), '#disabled' => $disabled, ); // Add extended language support option to content type form. $form['i18n']['i18n_node'] = array( '#type' => 'radios', '#title' => t('Extended language support'), '#default_value' => i18n_node_language_mode($form['#node_type']->type), '#options' => _i18n_node_language_options(), '#description' => t('If enabled, all defined languages will be allowed for this content type in addition to only enabled ones. This is useful to have more languages for content than for the interface.'), '#disabled' => $disabled, ); } } /** * Implements hook_form_BASE_FORM_ID_alter(). */ function i18n_node_form_node_form_alter(&$form, $form_state) { /** * i18n has to override locale.module * drupal_alter() fails to order modules correctly in some cases * for example specific hooks like hook_form_BASE_FORM_ID_alter * * its not possbile to reorder hook_form_BASE_FORM_ID_alter with * hook_module_implements_alter * * @see http://drupal.org/node/765860 */ // call a 'private' implemenation of i18n_node_form_node_form_alter() $form['#after_build'][] = '_i18n_node_form_node_form_alter'; } /** * Implements hook_form_BASE_FORM_ID_alter(). * Called by i18n_node_form_node_form_alter */ function _i18n_node_form_node_form_alter($form, $form_state) { $node = $form['#node']; if (i18n_node_type_enabled($node)) { if (!empty($form['language']['#options'])) { $form['language']['#options'] = i18n_node_language_list($node, TRUE); } } // Translate field names for title and body for the node edit form. $type = $form['#node']->type; if (!empty($form['title']['#title'])) { $form['title']['#title'] = i18n_node_translate_type($type, 'title_label', $form['title']['#title']); } if (!empty($form['body_field']['body']['#title'])) { $form['body_field']['body']['#title'] = i18n_node_translate_type($type, 'body', $form['body_field']['body']['#title']); } return $form; } /** * Implements hook_theme(). */ function i18n_node_theme() { return array( 'i18n_node_select_translation' => array( 'arguments' => array('element' => NULL), 'file' => 'i18n_node.pages.inc', ), ); } /** * List of language support modes for content. */ function _i18n_node_language_options() { return array( I18N_LANGUAGE_ENABLED => t('Normal - All enabled languages will be allowed.'), I18N_LANGUAGE_EXTENDED => t('Extended - All defined languages will be allowed.'), I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN => t('Extended, but not displayed - All defined languages will be allowed for input, but not displayed in links.'), ); } /** * Shorthand for translating node type strings * * @param $type * Node type name or full object */ function i18n_node_translate_type($type, $property = 'name', $source = NULL, $options = array()) { if (is_object($type)) { $source = $type->$property; $type = $type->type; } if (function_exists('i18n_string_translate')) { return i18n_string_translate(array('node', 'type', $type, $property), $source, $options); } else { return $source; } } /** * Translate node type name (filtered) */ function i18n_node_type_name($type, $name) { if (function_exists('i18n_string_translate')) { return check_plain(i18n_string_translate(array('node', 'type', $type, 'name'), $name)); } else { return check_plain($name); } } /** * Check whether this is a language enabled node type * * @param $type * Node, node type object, or node type name */ function i18n_node_type_enabled($type) { $type = is_object($type) ? $type->type : $type; return (bool)variable_get('language_content_type_'. $type, FALSE); }