Yet another question about not showing untranslated pages

Hi!

Yes, i know this topic has several inputs but i swear it making me crazy.

I m crafting a multilingual site where a section will have different pages if you’re in the spanish or french version . So i don’t need the autocomplete fallback translation between languages, not even the panel and of course not in the result page.

I’ve tried this:

$articles = $page->children()->visible()->filter(function($child) {
     return site()->language()->code() == $child->language()->code();
});

And i’ve tried this:

// Anything that matches a given language attribute

collection::$filters['languageIs'] = function($collection, $field, $value) {

  foreach($collection->data as $key => $item) {
    if(collection::extractValue($item->content()->language(), $field) != $value) {
      unset($collection->$key);
    }
  }

  return $collection;

};

$articles = $page->children()->visible()->filterBy('code', 'languageIs', 'en');

But nothing seems to be working. Any idea what i’m doing wrong or suggestion to try again?

Thank you very much for the help.

Yeah, this does not seem to work anymore. I’ll check it out.

Edit: This should work:

<?php
$articles = $page->children()->visible()->filter(function($child) {
  return $child->content(site()->language()->code())->exists();
});
1 Like

OMG! This works!! :tada: :tada: :tada: :tada:

As usual u are the best Texnixe :smiley:

1 Like

BTW is any chance to avoid autofill with default language content when user change between languages in panel? I’ve tried the translate: true in the blueprint and doesn’t work either

Now this topic is solved already but I did something similar to this today:

$children = $query->filter(function($page) {
  $content = f::read( $page->textfile() );
  if( empty( $content ) ) {
    return false;
  }
  return true;
});

If we are on and language and the page is not translated, it will be removed from $children. It will then only contain pages that have a translation.

I’m not too fond of the need for f::read tho.

The translate option should be set to false in that case?

I think i didn’t explain myself right.

I want to translate the fields, but i would like that when i change the language in the panel the fields would be empty (and not filled with the default language content which can be a little confusing).

Makes sense?

There is a pull request on GitHub for a translate: optional option, but this feature hasn’t been implemented yet.

1 Like

Oh, ok! So i’ll check it in the future :slight_smile:
Thank you again!