views:

239

answers:

2

Hello! I've made a content type for links, I'm trying to make a link directory. People only have to submit the link, description is voluntary. If no description is entered, I want the header that says "description" to disappear. The description field is the node body. Right now my snippet looks like this

<?php if (!empty($node->body)) {?> 
<div class="field field-type-link field-field-link-archive">
<h3>Description</h3>
<?php print $node->content['body']['#value'] ?></div>
<?php }?>

I expect this to check if node body is not empty, and if it isn't it'll print what's there. The problem is that the Description header is still printed out even if the node body is empty. Can anyone see what's wrong?

A: 

$node->body probably isn't empty

These are things that php considers to be empty

"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

do a var_dump on it to test it

And phps shorthand syntax was made for things like that. you shoudl use it it's much easier to read

<?php if(): ?>
// do stuff
<?php endif; ?>
Galen
I'm not that good with php yet, but I'll look into what foreach does. Thanks!
Toxid
i meant to put the shorthand for an if statement
Galen
Ah, I see, instead of the { } brackets I use : and endif;. You're right, it looks a lot better!
Toxid
+3  A: 

$node->body is whole node content with links, author info, post date etc.
You almost got it, see:

<?php if (!empty($node->content['body']['#value'])) {?> 
<div class="field field-type-link field-field-link-archive">
<h3>Description</h3>
<?php print $node->content['body']['#value'] ?></div>
<?php }?>

Also some wysiwyg-s modules automatically adds tags like p. Check it...

Nikit
Perfect! I was almost there, I actually tried $node->content['body'], but without the last value tag. Your snippet does exactly what I expected. Thanks!
Toxid