Monday 24 October 2011 11:46:13 am
In some cases, you might want to get the URL Alias of a single node in different languages at once. This of course can be done by fetching the content node in a dedicated language and then by accessing its url_alias attribute. But this is a bit overkill and this approach is also limited by the way eZ Publish handles multilanguage url aliases.
This is overkill because you would fetch your node (and query the database) several times (once by language you need), and limited because to properly get the url alias, you will need to have each language in your fallback examples.
Let me give an example. Say you have 2 languages, fre-FR and eng-GB. For some reason, you need to display french and english URL aliases at once for node #146. With the method described above, you could do:
{def $nodeFr = fetch( 'content', 'node', hash( 'node_id', 146, 'language_code', 'fre-FR' ) ) $nodeEn = fetch( 'content', 'node', hash( 'node_id', 146, 'language_code', 'eng-GB' ) )} <p>French URL Alias: {$nodeFr.url_alias} </p> <p>English URL Alias: {$nodeEn.url_alias} </p>
This example will work ONLY if eng-GB is a fallback language of fre-FR AND fre-FR is a fallback language of eng-GB:
site.ini in French siteaccess
[RegionalSettings] Locale=fre-FR ContentObjectLocale=fre-FR SiteLanguageList[]=eng-GB
site.ini in English siteaccess
[RegionalSettings] Locale=eng-GB ContentObjectLocale=eng-GB SiteLanguageList[]=fre-FR
..But you will still fetch the same entire node twice...
Since version 4.1, eZ Publish integrates a switchlanguage module which is located in kernel/private/modules directory. This module allows you to easily switch from a language to another, and therefore from a siteaccess to another:
http://www.mydomain.com/switchlanguage/to/<newLanguageSiteAccess>/<uri>
But this module also provides a useful fetch function to fullfill precisely our need:
{def $urlAliasEn = fetch( 'switchlanguage', 'url_alias', hash( 'node_id', $nodeId, 'locale', 'eng-GB' ) ) $urlAliasFr = fetch( 'switchlanguage', 'url_alias', hash( 'node_id', $nodeId, 'locale', 'fre-FR' ) )}
In PHP:
<?php $urlAliasEn = eZFunctionHandler::execute( 'switchlanguage', 'url_alias', array( 'node_id' => $nodeId, 'locale' => 'eng-GB' ) ); $urlAliasFr = eZFunctionHandler::execute( 'switchlanguage', 'url_alias', array( 'node_id' => $nodeId, 'locale' => 'fre-FR' ) );
And this fetch function does not require the fallback language stuff explained above...
Now this hidden feature is not hidden any more :)