I'm probably an idiot here, but is there a rule condition where I can check if a CCK field (specifically, a user reference), is empty or not empty?

Comments

mitchell’s picture

Component: Rules Core » Rules Engine
Status: Active » Fixed

For empty, use the text comparison condition with your user reference field and a blank comparison pattern. For not empty, do the same but also check the "negate" option.

obrienmd’s picture

Good call. I ended up just using some simple PHP.

colemanw’s picture

Status: Fixed » Active

Could you please share your PHP snippet?
The solution in #1 doesn't work, because both fields in the text comparison condition form are required (cannot be left blank).

obrienmd’s picture

In my case it was:

return $node->field_proad[0]['uid'] == NULL;

This returns TRUE and allows the rule if the "proad" user reference fields "uid" parameter is empty. For non-empty requirement, just change == to !=, or negate this condition in your rule setup.

obrienmd’s picture

Let me know if this makes sense... if not, give me some more details and I'll do what I can!

colemanw’s picture

Thanks, that is useful information. I ended up using the "field has value" condition, with this code as the value:
return array( 0 => array('value' => NULL));

I'm not sure which solution is more 'efficient' but it's good to know they both work.

obrienmd’s picture

Status: Active » Closed (fixed)

Not sure about proper workflow mgmt for support requests like this, but setting status to "closed" for now.

Lucience’s picture

Thank you very much, obrienmd!!!!

After hours of research, your code in post #4 solved my problem.


<?php if($node->field_picture_accessoires[0]['view'] != NULL) { ?>
<div class="accessoires">
<h3>Accessoires</h3>
<?php foreach ($node->field_picture_accessoires as $picture_accessoires) { print $picture_accessoires['view']; } ?>
</div>
<?php } ?>

I just don't understand, why I get an error message, when i close with an "endif".

Cheers
Lucience

sagar ramgade’s picture

Hi Lucience,

You are getting error when closing if statement with endif as you are using curly braces to open and close the if statement. If you want to use endif then if statement should open with colons ":" like this :

<?php if($node->field_picture_accessoires[0]['view'] != NULL) : ?>
<div class="accessoires">
<h3>Accessoires</h3>
<?php foreach ($node->field_picture_accessoires as $picture_accessoires) { print $picture_accessoires['view']; } ?>
</div>
<?php endif; ?>

Hope this clear your doubt.

Regards
Sagar

open social’s picture

you can use print null; in the second field in the rules interface, this will print nothing. While the requirement is forfilled.

leisurman’s picture

Instead of NULL, how can you rewrite this statement to return true if the field had data?

open social’s picture

if (isset($node->field_your_name[0]['value'])) {
return TRUE;
}
else {
return FALSE;
}
leisurman’s picture

Thank you!

elaman’s picture

Had same problem, but with random type fields. Came back here to share solution:

$filtered = array();
$function = $field['module'] .'_content_is_empty';
foreach ((array) $node->$field['field_name'] as $delta => $item) {
  if (!$function($item, $field)) {
    $filtered[] = $item;
  }
}
if (empty($filtered)) {
  // field is empty
}   
cmsproducer’s picture

Component: Rules Engine » Rules Core

We discussed this issue in a PHP community especially to determine a way that can give a true response in situations where the field might be empty (in human terms), but there is some markup in it (&nbsp,

amaisano’s picture

The first proposed solution will work (doing it using Textual Comparison) if you check "Evaluate as RegEx" and enter ^$ as the pattern, which matches an empty string.