Blog-plugin: Show Only Translated Posts?

Created on 16 Jun 2017  路  3Comments  路  Source: rainlab/blog-plugin

Is it possible to only show translated posts when navigated to a specific locale?

I want to show only default translations under /en , I want to show only Polish translated posts under /pl.

It doesn't make sense that another language is having default translated posts pulled through if you have specifically navigated away from the default locale.

I tried modifying the blogPosts component, however it's not working as expected. If I add a new language and resave a post, it reappears due to the rainlab_translate_attributes being created, event though the attributes are empty .

What am I doing wrong or is there a working method to this?

$posts = BlogPost::
join('rainlab_translate_attributes', 'rainlab_blog_posts.id', '=' , 'rainlab_translate_attributes.model_id')
->where('rainlab_translate_attributes.locale', $this->activeLocale)
->where('rainlab_translate_attributes.model_type', 'Ranlab\Blog\Models\Post')
->with('categories')
->listFrontEnd([
                    'page'       => $this->property('pageNumber'),
                    'sort'       => $this->property('sortOrder'),
                    'perPage'    => $this->property('postsPerPage'),
                    'search'     => trim(input('search')),
                    'category'   => $category,
                    'exceptPost' => $this->property('exceptPost'),
]);
Question

Most helpful comment

Ok, your way is probably the right one in how to get filtered list of translated posts only.


I was thinking about more simple approach on template level (page/layout) and this is how I would solve it:

````php

use \RainLab\Blog\Models\Post;

function onStart() {

$posts = Post::all();

foreach( $posts as $key => $post) {

    $post->noFallbackLocale()->lang('en');

    if( empty($post->title) ) {
        unset($posts[$key]);
    }

}

$this['filteredPosts'] = $posts;

}

?>
````

Definitelly not the best solution but should work :)

All 3 comments

Sorry I have no time to test it now, but have you tried noFallbackLocale() method?

Yes I tried this and only appears to work on individual attributes - the post still appears but the attributes are empty.

There needs to be a way to only pull in posts that have a translation.

I created a new behavior function and it works - however the amount of queries being made to the DB checking translations is crazy. It seems to be the same using the default translate feature. I'm seeing up to 130 queries being made to the DB but that's an issue I should post on the translate plugin repo.

public function scopeCheckTransWhere($query, $locale = null)
    {


        if (!$locale)
        {
            $locale = $this->translatableContext;
        }
        if($locale == 'en'){

            return $query;
        }

        $query->select($this->model->getTable() . '.*');

        $query->where(function ($q)
        {
            $q->where('rainlab_translate_indexes.item', '=', 'slug');
        });

        $query->leftJoin('rainlab_translate_indexes', function ($join) use ($locale)
        {
            $join
                ->on(Db::raw(DbDongle::cast($this->model->getQualifiedKeyName(), 'TEXT')), '=', 'rainlab_translate_indexes.model_id')
                ->where('rainlab_translate_indexes.model_type', '=', get_class($this->model))
                ->where('rainlab_translate_indexes.locale', '=', $locale);

        });

        return $query;
    }

Ok, your way is probably the right one in how to get filtered list of translated posts only.


I was thinking about more simple approach on template level (page/layout) and this is how I would solve it:

````php

use \RainLab\Blog\Models\Post;

function onStart() {

$posts = Post::all();

foreach( $posts as $key => $post) {

    $post->noFallbackLocale()->lang('en');

    if( empty($post->title) ) {
        unset($posts[$key]);
    }

}

$this['filteredPosts'] = $posts;

}

?>
````

Definitelly not the best solution but should work :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rickmills picture rickmills  路  5Comments

daveharrisonnet picture daveharrisonnet  路  9Comments

karnold picture karnold  路  5Comments

sabuz picture sabuz  路  5Comments

HitArrowLegend picture HitArrowLegend  路  6Comments