Filter untranslated content in Kirby 3

What is the best way to filter out untranslated pages in Kirby 3?

This worked in Kirby 2, but not in 3:

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

There’s a $page->translations() method that might be useful, haven’t tested that yet. Have a look what dump($page->translations()) gives you.

Edit: no, unfortunately, that only returns the languages, not the actual available translations.

$page->readContent() returns an empty array if there is nothing in the content file of the given language:

dump($page->readContent('de'));

Edit: found a better one:

var_dump($page->translation($kirby->language()->code())->exists());

So that gives us:

<?php
	$articles = page('articles')->children()->visible()->flip()->filter(function($child) {
		return $child->translation($kirby->language()->code())->exists());
	});

Hm, that gives me an error because $kirby is undefined inside the filter function.

(I edited the code block, there was an additional bracket at the end).

Then with $kirby defined:

<?php
	$articles = page('articles')->children()->visible()->flip()->filter(function($child) use($kirby){
		return $child->translation($kirby->language()->code())->exists();
	});

Or without use, using kirby() instead of $kirby inside the callback.

1 Like

Perfect, many thanks :+1: