Search code examples
drupal-7

How to get node path from id on multi lingual website?


I have a node, with nid. i.e. 78 and it's on French language (main language is English) so local name to it is:

fr/node/78

or just

node/78

?

Also that page has some alias, i.e. "hello" so it's relative path would be:

/fr/hello

How can I get that relative path?

Yes, I know that:

$alias = drupal_get_path_alias('node/78');

should give me my page alias, but it's not working, probably because of different language?!? Or that should be:

$alias = drupal_get_path_alias('fr/node/78');

I tried both, but none of them is working?!? I can't believe that there is no simple function available, which would take node id as parameter and return SEO (with alias) page path?!?

Please help. What is the easiest way to get node seo path on multi lingual website if I have nid?


Solution

  • Finally I got it. In case somebody else strugles with this:

    // Returns root relative path
    function get_node_url_ml ($nid, $language){
        $defaultLanguage = language_default();
        $local_path = 'node/' . $nid;
        $url = drupal_lookup_path('alias', $local_path, $language);
        if (!$url)  $url = 'node/' . $nid;
        if ($language != $defaultLanguage->language) $url = $language.'/'.$url;
        return '/'.$url;
    }