Can someone tell me how to write the conditional statement to determine if a node reference field contains no value? Formerly in D6 I used this this to determine if there was some value in the field, and if there was a value then I'd launch certain results:

<?php if($node->field_nodereference['0']['nid']): ?>
  Do something if it contains a value.
<?php else: ?>
  Do something else if it does not contain a value.
<?php endif; ?>

I've been looking for D7 examples for hours and I've not found out how to accomplish the same thing. The code below I have tried with all kinds of if, empty, !empty, isset, !isset, you fricken name it. But this is all I've got, and while I can get some results for when there is a value, I cannot get no results for when there is not a value.

<?php if($content['field_nodereference']): ?>
  Do something if it's no empty.
<?php else: ?>
  Do something else if it does not contain a value.
<?php endif?>

Anyone have a guess here?

Comments

seaneffel’s picture

You know, I suspect that setting the node_reference field to a select list value of "none" does not mean that there is no value in the field. The upper example from D6 seems to look for a nid value in the node_reference field, where my D7 just checks for any value. Could that be what's going on?

What's the format to check for a nid in the D7 node reference field?

seaneffel’s picture

Title: How to determine if D7 node reference is empty in node.tpl.php? » Check for nid value in node reference field for node.tpl.php?
Status: Active » Closed (fixed)

Figured it out. The key was pinning down the structure of the field to test if the field had a value for nid (meaning that the node reference field was actually referencing a real nid). Recorded here for posterity.

<?php if (isset($content['field_nodereference']['#items']['0']['nid'])):?>
	Do something.
<?php endif; ?>
WorldFallz’s picture

Sorry I didn't see this sooner! Good job figuring it out.