Jump to content
lele_ballack

How to tell selection to avoid empty fields

Recommended Posts

Hi everyone, 

I'm trying to build an index with all the fields' name that belong to a template, excluding those fields that are empty. 

<?php
 $i=0;
 foreach ($page->fields->find('limit=3') as $field) {
 	++$i;?>
    <li>
      <a class="list-item" href="#par-0<?php echo $i;?>" title="<?=$field->name?>"><?=$field->name?></a>
    </li>
<?php } ?>

Are there ways to do it in the selector?

Thank you 🙂

Share this post


Link to post
Share on other sites
6 hours ago, lele_ballack said:

Are there ways to do it in the selector?

No, because you are finding Field objects these are neither empty nor not empty.

Instead you need to check the values of the field names for the current page to see which are empty. Something like this...

$i = 1;
foreach($page->fields as $field) {
	// Limit to 3 non-empty field values
	if($i > 3) break;
	// Check if value is empty
	$value = $page->getFormatted($field->name);
	if($value instanceof WireArray) {
		if(!$value->count) continue;
	} else {
		if(!$value) continue;
	}
	// Do whatever with $field here
	echo "{$field->name}<br>";
	$i++;
}

 

  • Like 4

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...