Find all Blog Posts that have a translation in a specific locale
1
If you need to find all Blog Posts that have a translation in a specific locale, use this:
$locale = 'fr';
$posts = RainLab\Blog\Models\Post::whereHas('translations', function ($query) use ($locale) {
$query->where('locale', $locale)->whereRaw("attribute_data not like '%\"content\":\"\"%'");
})->get();
Note: this obviously does not work for the DEFAULT locale
This can easily be integrated with the blogPosts component by adding this in your PHP code section:
function onStart()
{
$locale = $this->activeLocale;
if ($locale === 'fr') {
$untranslatedPostIds = RainLab\Blog\Models\Post::whereDoesntHave('translations', function ($query) use ($locale) { $query->where('locale', $locale)->whereRaw("attribute_data not like '%\"content\":\"\"%'"); })->lists('id');
$this->blogPosts->setProperty('exceptPost', $untranslatedPostIds);
}
}
Hi, how do you add this code? On the blogposts partial? on the page where there will be displayed? Merci!