Closed (fixed)
Project:
References
Version:
7.x-2.0
Component:
Code: node_reference
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
19 Feb 2012 at 23:15 UTC
Updated:
19 Mar 2012 at 17:50 UTC
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
Comment #1
seaneffel commentedYou 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?
Comment #2
seaneffel commentedFigured 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.
Comment #3
WorldFallz commentedSorry I didn't see this sooner! Good job figuring it out.