Apply filter only when languages exists

Hey together,

I’m using this if statement to filter articles by a tag. This works as long as I have a multi language setup. I want to archive that I can delete the multi language setup without having to touch this code again. This would mean I have to do a check if languages exists before I use filter().

if (urldecode(param('tag'))) {
    $articles = $page->children()
                      ->listed()
                      ->filter(function ($child) { 
                          return $child->translation(kirby()->language()->code())->exists(); 
                      })
                      ->filterBy('tags', $tag, ',')
                      ->sortBy('date')
                      ->flip();
  }

I’m trying to find a solution by myself but all I came up with is this workaround which works but is not the proper way I guess. :smiley:

->filter(function ($child) { 
    return kirby()->languages()->isNotEmpty() ? $child->translation(kirby()->language()->code()) : $child->template() == "article"; 
})

I wouldn’t know, but I do similar things in one of my sites:

if(!is_null(kirby()->language($langCode))) {

But that’s basically what I do right now I think. The problem I have is that filter() always applies. I’m trying to call filter() only when a languages exists but that would need some sort of a check beforehand and I don’t know how to do this …

The condition is perfectly alright, but the bit where you check for the template is unnecessary or would even be not what you wanted if your children had different templates (in this case you probably haven’t but still):

$articles = $articles->filter(function($child) {
  return kirby()->languages()->isNotEmpty() ? $child->translation(kirby()->language()->code()) : $child; 
});

Ok that was easy… I tried null before but that obviously didn’t work. Thank you @texnixe :blush: