Changeset 30

Show
Ignore:
Timestamp:
02/21/09 20:26:24 (2 years ago)
Author:
blundeln
Message:

Updated module so translation doesn't happen when node loaded for editing. Please check it works. Users must use translation page (as matthew found) to edit pages. Edit tab will take longer to fix, but can probably be done in template variables.
Please confirm the bug is fixed. If not, I'll take another look.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • drupal/modules/simple_translation/trunk/TODO

    r11 r30  
     1- Translation of CCK text fields 
     2- Translated attachments if they exist 
  • drupal/modules/simple_translation/trunk/patches/menu.inc.drupal-6.5.tree-item-hook.patch

    r11 r30  
    66   while ($item = db_fetch_array($result)) { 
    77 
    8 +    // This hook allows modules to modify menu items before rendering. 
     8+    // Nick Blundell 2008: This hook allows modules to modify menu items before rendering. 
    99+    foreach (module_implements('menu_tree_item_alter') as $name) { 
    1010+      $function = $name .'_menu_tree_item_alter'; 
  • drupal/modules/simple_translation/trunk/simple_translation.module

    r11 r30  
    99if (!function_exists("d")) { 
    1010  function d($message, $item=null, $print=false) { 
     11    global $user; 
     12    if ($user->uid != 1) { 
     13      return; 
     14    } 
    1115    if ($item) { 
    1216      $message .= ": " . print_r($item,true); 
     
    2933 
    3034function simple_translation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
    31  
     35  // Don't translate when we are editing a node. 
     36  if (dont_translate()) { 
     37    return; 
     38  } 
    3239 
    3340  global $language; 
     
    3744    return; 
    3845  } 
    39    
     46  
    4047  // Load the translation source node. 
    4148  $sNode = safe_node_load($node->tnid); 
     
    5663  // Replace the loaded node with the translated node. 
    5764  //$node = $tNode; 
     65  //d("source node: $node->nid - trans node: $tNode->nid"); 
    5866  $node->title = $tNode->title; 
    5967  $node->body = $tNode->body; 
     
    8088    // Replace the menu title with the translated title. 
    8189    // Remember, we hook node_load in this module to translate it. 
     90    // TODO: Would be more efficient to make a DB call to get title rather than node_load 
    8291    $node = node_load($nid); 
     92    //d("Translating title ".$item["link_title"]. " to trans node title $node->title"); 
    8393    $item["link_title"] = $node->title; 
    8494    return; 
     
    93103function simple_translation_db_rewrite_sql($query, $primary_table, $primary_key, $args = array()){ 
    94104  // Adapted from i18n module 
     105  
     106  // Contrary to i18n, only act on views queries, so we don't break other system queries. 
     107  if (!array_key_exists('view',$args)) return; 
    95108   
    96109  $query_mods = array(); 
     
    112125 
    113126      // Show only translation sources from translation sets. 
    114       $query_mods["where"] = "$primary_table.nid = $primary_table.tnid OR $primary_table.language = ''"; 
    115   } 
    116    
     127      $query_mods["where"] = "$primary_table.tnid = 0 OR $primary_table.nid = $primary_table.tnid OR $primary_table.language = ''"; 
     128  } 
     129  //d("query_mods", $query_mods);  
    117130  return $query_mods; 
    118131} 
     
    120133 
    121134function simple_translation_views_pre_render(&$view) { 
    122   //d("result",$view->result); 
    123135  foreach ($view->result as $item) { 
    124136    if (!$item->nid) { continue; } 
     
    141153/* Initialise the module. */ 
    142154function simple_translation_init() { 
    143    
     155 
    144156  // Need to clear the menu cache to allow menu translation. 
    145157  // TODO: Need to find a better way. 
     
    155167 * ======================================= 
    156168 */ 
     169 
     170 
     171function dont_translate() { 
     172  $path = $_GET['q']; 
     173   
     174  if (stristr($path, "/translate")) { return true; } 
     175  if (stristr($path, "/edit")) { return true; } 
     176  //d($path); 
     177  return false; 
     178} 
    157179 
    158180/* Loads a node in such a way that it will not be processed recursively by our nodeapi hook. */ 
     
    168190/* Loads the translated version of a node or returns the original node if no translation. */ 
    169191function _load_translated_node(&$node, $language) { 
    170   
     192 
     193  //return $node; 
     194 
    171195  // Return original if no trans source, non-lang specific node, or the node language is same as user language. 
    172196  if (!($node->tnid and $language->language) or $language->language == $node->language) { 
     
    187211 
    188212 
    189 /*  
    190  * ======================================= 
    191  * OLD STUFF 
    192  * ======================================= 
    193  */ 
    194  
    195 function xxxsimple_translation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
    196   // TODO: When incomming node is a translated node, comments of trans source are not dispayed. 
    197  
    198   global $language; 
    199  
    200   if ($op != "alter") { 
    201     return; 
    202   } 
    203  
    204   // State what the arguments are. 
    205   $teaser = $a3; 
    206   $page = $a4; 
    207  
    208   // Get the translation source node of this node 
    209   $sNode = node_load($node->tnid); 
    210  
    211   if (!$sNode) { 
    212     return; 
    213   } 
    214  
    215   // Get the translated node. 
    216   $tNode = _load_translated_node($sNode, $language); 
    217    
    218   if (!$tNode) { 
    219     return; 
    220   } 
    221  
    222   // Sadly need to re-build the node's content, since drupal_render cannot be called more than once on content. 
    223   $sNode = node_build_content($sNode, $teaser, $page); 
    224  
    225   // Render the node and update the originally rendered node. 
    226   $tNode = node_build_content($tNode, $teaser, $page); 
    227    
    228   // Render the content in the correct language. 
    229   $sNode->content["body"] = $tNode->content["body"]; 
    230   $renderedContent = drupal_render($sNode->content); 
    231   $sNode->teaser = $renderedContent; 
    232   $sNode->body = $renderedContent; 
    233    
    234   // Update the page title. 
    235   $sNode->title = $tNode->title; 
    236   drupal_set_title(check_plain($sNode->title)); 
    237  
    238   // Update the links 
    239   $sNode->links = module_invoke_all('link', 'node', $tNode, $teaser); 
    240   drupal_alter('link', $sNode->links, $tNode); 
    241  
    242   // Replace $node with the modified $sNode 
    243   $node = $sNode; 
    244 } 
    245  
    246  
    247  
    248  
    249213?>