Jump to content

selector for not empty value

Recommended Posts

which selector would select all items where a certain field is not empty?

foreach ($items as $item) :
echo $item->numChildren("body!=''");

I think it's clear for a human just by looking at this what I'm trying to do, doesn't work though.

I have to add, it's a multilingual field so the same field in another language might not be empty, not sure that's relevant though… 

Thanks for help!

Share this post

Link to post
Share on other sites

Thanks for hints, it works but not always.

Anyhow, what I'm trying to do is to return the number of descendants (with a non-empty body-field) for each language on the same page.

Here's what I'm doing…

foreach($languages as $language) :
    $hreflang = $homepage->getLanguageValue($language, 'name'); 
    $url = "/$hreflang/$page->name/";
    $descendants = $pages->get($url)->descendants("template=cast, body!=");
    $total = count($descendants);
    $total = "($total events)";
    <a hreflang="<?=$hreflang;?>" href="<?=$url;?>"><?=$language->title; ?></a> <?=$total;?>

Somehow it ignores the specific path I'm using to find the descendants and translates it to the currently active language, thus returning the same results for each language.

I can tell because when I echo all the results…

foreach ($descendants as $d) :
    echo $d->url; 

it always returns: /en/page-name/ and never /de/page-name/

Am I at least right in my assumption?

Thanks for help!

Share this post

Link to post
Share on other sites

switch the language inside the foreach so it switches the user's language each time it returns the variable?

The thing is, we might add more languages later, so I'd like to find a solution that will include all the languages from the get go.

Share this post

Link to post
Share on other sites

I got the empty-body requirement working, simply with a selector like ("body!=") – works fine.

However, still struggling to return results in one language on another language, i.e. showing german results with no empty body on the english site.

I followed this advice:

On 10/6/2020 at 5:34 PM, Matzn said:

and here's my code

$language = $user->language; // save the user's language
$languages = $page->getLanguages();
$user->language = $languages->get("de");  // returns 'Deutsch'
$descendants = $page->descendants("template=cast, body!=");
$total = count($descendants);
$total = "(".$total." ".__('events').")";
echo $user->language->title.' '.$total; // returns 'Deutsch: (0 events)' when on english page :(
$user->language = $language; // restore the user's language

but it still doesn't work.

Any ideas?

I don't know how to select the german version of the page nor how to loop them.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...