The problem is - I can't figure out how to switch to translated slug:
I've implemented multilingual slugs using lav45/yii2-translated-behavior
.
Database is quite simple:
Portfolio model:
id | created_at | is_active
PortfolioLang model:
portfolio_id | lang_id | title | content
So far - so good. I have a actionView
that calls out translated content as follows:
protected function findModelBySlug($slug)
{
if (($model = Portfolio::findOne(['slug' => $slug])) !== null) {
return $model;
} else {
throw new NotFoundHttpException();
}
}
public function actionView($_lang, $slug)
{
$portfolioLang = PortfolioLang::findOne(['lang_id' => $_lang,'slug' => $slug]);
$model = $portfolioLang->portfolio;
$pictures = PortfolioPicture::find()->where(['portfolio_id' => $model->id])->orderBy(['sorting'=>SORT_ASC])->all();
return $this->render('view', [
'model' => $model,
'picture' => $pictures,
'langList' => Lang::getList(),
]);
}
Must note, that this is working, "until" it try to switch between languages while I'm opened the entry.
I was using example code from here: https://github.com/lav45/yii2-translated-behavior-demo/blob/master/frontend/components/LangHelper.php
I guess that I need something else than a sample code (it looks like it works only for current ID's for existing entries). Could you, someone, give me some hints for the next step?
Thanks in advance!
Found answer to my own question. Maybe it helps someone. So - created some LangHelper that gets slugs from DB (if there is some) with some extras: https://gist.github.com/nedarta/07071b7cb7d20f134318b593e53d9e50