Search code examples
drupalinternationalizationdrupal-6drupal-modules

Drupal 6: Show Flags for Node Translations


For each node preview, I want to have little flag icons at the top representing the available translations. I have seen the language switcher code, but it outputs all the languages all the time. That is annoying because people will click their language and then find that the page is only available in English anyway (I have a site with many articles in a great variety of languages). I have seen this done though. I'm relatively new to Drupal programming. Can anyone give me a pointer?

Thanks!


Solution

  • Figured it out by myself, and noting it here because I know I'm not the only one with this issue.

    The template I'm working off is called scaccarium, so I went to /themes/scaccarium/template.php and added the following function:

    function scaccarium_preprocess_node(&$vars) {
      $node = $vars['node'];
      $translationlinks = array();
      // Move translation links into separate variable
      foreach ($node->links as $key => $value) {
        if ($value['attributes']['class'] == 'translation-link') {
          $translationlinks[$key] = $value;
          // unset($vars['node']->links[$key]);
        }
      }
      $vars['translationlinks'] = theme('links', $translationlinks, array('class' => 'links translationlinks inline'));
    }
    

    If your template is called something else, you should obviously go to a different folder and also change the first word of the function name. And if your theme comes with an existing _preprocess_node function, carefully modify it.

    Then, I went to my template's node.tpl.php and I added

    <?php if ($translationlinks) {
                print $translationlinks;
          } ?> 
    

    next to the title.

    Then I had to clear the Drupal cache (even though caching was disabled!) in Performance > Caching in order to get this to work.

    Done!

    ... To add language links at the top of full nodes, I had to add another "print $translationlinks" at a different place in node.tpl.php as well, as the h3 title thing was just for node previews. Then, to remove the redundant language links at the bottom of full nodes, I tried that unset line that you see commented out in template.php - I found that it had no effect even though another website had recommended it. So what I ended up doing was using CSS for this, adding the following to my template's CSS file:

    .node-links .translation-link {
      display: none;
    }
    

    I hope that my experience will help someone else with the same problem.