/[drupal]/contributions/modules/select_translation/select_translation.module
ViewVC logotype

Diff of /contributions/modules/select_translation/select_translation.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2, Wed Jun 18 16:04:38 2008 UTC revision 1.3, Tue Sep 16 13:21:43 2008 UTC
# Line 12  function select_translation_help($sectio Line 12  function select_translation_help($sectio
12  }  }
13    
14  /**  /**
15     * Implementation of hook_views_api
16     */
17    function select_translation_views_api() {
18      return array(
19        'api' => '2.0',
20        'path' => drupal_get_path('module', 'select_translation'),
21      );
22    }
23    
24    /**
25   * Returns the node_id representing the preferred translation of the given node.   * Returns the node_id representing the preferred translation of the given node.
26   *   *
27   * $mode can be :   * $mode can be :
# Line 22  function select_translation_help($sectio Line 32  function select_translation_help($sectio
32   * - original ; in which case the function will return the node in the current   * - original ; in which case the function will return the node in the current
33   *              language if available ; if not that in the original language.   *              language if available ; if not that in the original language.
34   *   *
35     * - an array of language codes ; in which case it will return the first
36     *             available translation. In that array, the special value
37     *             'current' referes to the current language,
38     *             'default' refers to the default language, and 'original' to the
39     *             node's original language. If not match is found the version
40     *             in the original langauge is returned.
41     *
42   */   */
43  function select_translation_of_node($nid, $mode = 'default') {  function select_translation_of_node($nid, $mode = 'default') {
44    global $language;    global $language;
45    
46    $node = node_load($nid);    $node = node_load($nid);
47    if (!$node || $node->tnid == 0 || $node->language == $language->language) {    if (!$node || $node->tnid == 0) {
48      return $nid;      return $nid;
49    }    }
50    
51    $translations = translation_node_get_translations($node->tnid);    // Prepare input
52    if (isset($translations[$language->language])) {    if (!is_array($mode)) {
53      return $translations[$language->language]->nid;      if ($mode == 'default') {
54    } else if ($mode == 'default' && isset($translations[language_default('language')])) {        $mode = array('current', 'default');
55      return $translations[language_default('language')]->nid;      } else if ($mode == 'original') {
56    } else {        $mode = array('current');
57        } else {
58          $mode = array($mode);
59        }
60      }
61      $mode[] = 'original';
62      $mode = array_unique($mode);
63    
64      foreach ($mode as $i => $v) {
65        if ($v == 'default') {
66          $mode[$i] = language_default('language');
67        } else if ($v == 'current') {
68          $mode[$i] = $language->language;
69        }
70      }
71    
72      // Try to see if we can return the right one without having to load anything more.
73      if ($mode[0] == $node->language) {
74        return $nid;
75      }
76      if ($mode[0] == 'original') {
77      return $node->tnid;      return $node->tnid;
78    }    }
79    
80      $translations = translation_node_get_translations($node->tnid);
81      foreach ($mode as $m) {
82        if ($m == 'original') {
83          return $node->tnid;
84        } else if (isset($translations[$m])) {
85          return $translations[$m]->nid;
86        }
87      }
88      return $node->tnid;
89  }  }
90    

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.2