Getting language composition link #2177
-
Hi, I'm building a multilingual website and using the built-in composition elements together with the CMS functionality to create language versions. I came across What I am looking for is a method like I looked through the code of Starting with foreach (Yii::$app->adminLanguage->getLanguages() as $lang) {
$array[] = [
'lang' => $lang,
'item' => Yii::$app->menu->find()->where(['nav_id' => $currentMenuItem->navId])->lang($lang['short_code'])->with('hidden')->one(),
];
} and then processing this in the foreach (self::getDataArray() as $langData) {
$item = $langData['item'];
$lang = $langData['lang'];
$isActive = $currentLang == $lang['short_code'];
if ($item) {
if ($item->type == NavItem::TYPE_MODULE && !empty($rule)) {
$routeParams = [$rule['route']];
foreach ($rule['params'] as $key => $value) {
$routeParams[$key] = $this->findUrlRuleParamValue($lang['short_code'], $key, $value);
}
$compositionObject = Yii::createObject(Composition::class);
// https://github.com/luyadev/luya-module-cms/issues/48
// $compositionObject->off(Composition::EVENT_AFTER_SET);
$compositionObject['langShortCode'] = $lang['short_code'];
$link = Yii::$app->urlManager->createMenuItemUrl($routeParams, $item->id, $compositionObject);
} else {
$link = $item->link;
}
} else {
$link = Yii::$app->urlManager->prependBaseUrl($lang['short_code']);
}
$items[$lang['short_code']] = $this->generateHtml($this->ensureHostInfo($link, $lang), $isActive, $lang);
unset($item, $lang);
} So is this the only way to get all the alternate links or is there a simple composition method for this? Couldn't find anything in the documentation and it just seems pretty chaotic to run with the above. Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @grrinch Would you like to link to a module route or to a cms page from the menu?
This looks like you are in view file or a controller? Are URL Rules used? Please make a full example where you like to link at. |
Beta Was this translation helpful? Give feedback.
-
@grrinch i also want to mention that the task of mixin modules and cms routes is very complex. There are also different options on how to add your module. You can add the module by using a module block or declare a page as module, i assume you saw this in the CMS Admin UI. This can also change the behavior of how you generate and interact with routes. |
Beta Was this translation helpful? Give feedback.
Hi @grrinch
Would you like to link to a module route or to a cms page from the menu?
This looks like you are in view file or a controller? Are URL Rules used? Please make a full example where you like to link at.