Get Mapped Page by cID and Language / Locale

Permalink 1 user found helpful
Hi,

I needed to hard code some links in my template, but was running into an issue where changing the url paths in alternate languages would cause the links to fail.

My current solution is to create a config variable with an array of the cIDs of each page I want to link to in each language and then when I need to make a link, I execute the following:

<?php echo URL::to(Page::getCollectionPathFromID(Config::get('concrete.rockys.special_offers_page.'.Localization::activeLanguage()))); ?>


It works, but it seems cumbersome. Ideally, I would like there to be a function to find the page in an alternate language based on the given cID and the given locale or language string. i.e.

Page::getCollectionMultilingualPathFromID($cID, $local);


Any chance of this in a future version?

 
mlocati replied on at Permalink Reply
mlocati
The future is now ;)

You already can retrieve the page in language Y associated to a page in language X.

What you could to is something like this:
// $pageInLanguageX is the page in the language X
// 'Y' is the destination language
$sectionInLanguageY = \Concrete\Core\Multilingual\Page\Section\Section::getByLocale('Y');
if (isset($sectionInLanguageY)) {
    $idOfThePageInLanguageY = $sectionInLanguageY->getTranslatedPageID($pageInLanguageX);
    if ($idOfThePageInLanguageY) {
        $pageInLanguageY = Page::getByID($idOfThePageInLanguageY);
        if (is_object($pageInLanguageY) && !$pageInLanguageY->isError())) {
            // Do what you want with $pageInLanguageY
        }
    }
}
a575606 replied on at Permalink Reply
That was super useful, I really appreciate it :). Works perfectly.
arwahafdhi replied on at Permalink Reply
How do you make it works? it doesn't work for me, what did you put in each variable:
Y, $pageInLanguageX