2

I have 2 content bundles for node entity:

  1. Custom Layout Pages (custom_layout)
  2. Recipes (recipe)

Then I created two content using those content types, Potato (recipe) and Homepage (custom_layout).

I have 2 languages for both content, en and fr. I'm trying to load the potato recipe node inside of the homepage content.

So inside the preprocess hook, I have this code

... if(isset($vars['node']) && $vars['node']->type === 'custom_layout'){ $recipe_id = 10; $vars['recipe_side'] = render(node_view(node_load($recipe_id))); } ...

Then I echo the $recipe_side in the custom_layout template file.

When I load the fr version of the page, I expected the recipe to be loaded in fr as well, but it always displays the en version.

I'm guessing it's because the en recipe node id is 10, and it doesn't load the translated node.

If that's the case, how can I make it work so that the loaded node follows the current language?

4
  • I'm guessing it's because the en recipe node id is 10, and it doesn't load the translated node. >> Yup the translated node will also its dedicated node id.So for fr language you will have to pass the nid of fr language recipee node. Commented Mar 11, 2015 at 6:59
  • Drupal's core Locale module has a function api.drupal.org/api/drupal/… for this. Otherwise you will need to load the node programmatically and inside the node object, you'll have details about your translated nid.
    – Kevin
    Commented Mar 11, 2015 at 6:59
  • I saw your comment after posting my solution, which is taking from my reservoir. This solutions is preferred and works charmingly.
    – Cesar
    Commented Sep 14, 2016 at 9:42
  • Once again a very old answer getting to the top list? ( asked 1 year ago ). Common !!
    – Cesar
    Commented Sep 14, 2016 at 9:42

2 Answers 2

1

Try this code:

 $node_trad = node_load($variables["node"]->nid); // in your case, 10.
 $idi_trad = translation_node_get_translations($node_trad->tnid);

  $vars['recipe_side'] = render(node_view(node_load($idi_trad["fr"]->nid)));
  //$vars['recipe_side'] = render(node_view(node_load($idi_trad[$language->language]->nid)));
  // alternative, more dynamical                

Essentialy you load the node in original language, use the translation function to get all available translations, and use that what you need. Instead of "fr" you could use language->language instead to make it completely dynamical.

0

Use the following code to get the TRANSLATED node for the current language:


$node_id = 10;  // Put the node ID here where you want the translation for.
$translation_node = translation_node_get( $node_id );

// Show the translated node
print_r( $translation_node );

function translation_node_get( $node_id )
{
    if ( is_numeric($node_id) && $node_id > 0 )
    {
        // Get current language code
        global $language;
        $lang_code = $language->language;

        // Get translation node id
        $base_node = node_load( $node_id );
        $tnid = $base_node->tnid;

        // Search the database for the translated node of the current language
        $result = db_select('node', 'n')
        ->fields('n', array('nid', 'type', 'uid', 'status', 'title', 'language'))
        ->condition('n.tnid', $tnid)
        ->condition('n.language', $lang_code)
        ->addTag('node_access')
        ->execute();

        foreach ( $result as $trans_node )
        {
            // Return the correct language node
            return node_load( $trans_node->nid );
        }
    }

    // No translation node found!
    return false;
}

1
  • 1
    Hello. Could you add some explanation? For the new users who want to understand, not only copy-paste? :)
    – Mołot
    Commented Jun 19, 2015 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.