How to disable post or page for some languages?

pwrobel - 1 month ago

Hi! I've got a small question. I work with multilingual page, but I need to disable some post and pages in some languages. I know that I can leave fields empty, that works, but when I edit post or page, empty fields in other languages are filled with default language content. How can I fix it?

samharrison7 - 1 month ago

There might be an easier option than this, but one approach would be to assign a field to your post/page that gets the user to select if the post/page is enabled in a specific locale (e.g. it could be checkboxes for the user to select any locales they don't want their page to be available for). Then do a check in the post/page view to see if the current locale - which you can get with {{ config_get('app.locale') }} - is in the allowed list. You'd obviously have to do a similar thing for post/page listings if you only want the ones allowed in a specific locale to be listed.

E.g. if you had a boolean field disable_for_fr for a post, in your post view you could have:

{% if post.disable_for_fr.true and config_get('app.locale') == 'fr' %}
    {# Sorry, this post isn't available in French. #}
    Désolé, cet article n'est pas disponible en français.
{% else %}
    {# Show the post #}
    {{ post.content.render()|raw }}
{% endif %}

P.S. Overriding views can be done via a service provider, e.g.

protected $overrides = [
        "anomaly.module.posts::posts/view"       => "theme::posts/view",

pwrobel - 1 month ago

Thanks for reply. I thought about this method and this is my plan B at this moment. I can filter post or pages easier by title (when title is not empty then show post). Main problem -> empty fields are going to be autocomplited by application. Where can I disable it?

ryanthompson - 1 month ago

@pwrobel I would go about it with a middleware defined in config/streams.php and a flag / field on the posts that you can read. If you handle the response first then you can inject / look in the ViewTemplate super variable that get's passed to views and grab the post / do your testing that way. If all is well return the response.

Basically the equivalent of what Laravel used to call "after filters".

This way you can keep your logic out of the view and control the response better overall.

pwrobel - 1 month ago

I found easier way: translatable select field type with enabled and disabled options. It works fine at this moment. Thanks for help.