0

The following PHP in a node tpl file:

check_plain($node->field_room[$node->language][0]['value'])

Will print this in the page: Value1

What I want however is if the field has a value of 'Value1' then I want to print something else on the page. Ive tried the following but I think I have a syntax error:

if ( check_plain($node->field_room[$node->language][0]['value']) == "Value1" ) {
                        echo "<p>Print something else </p>";
                    }
1
  • You should not need the check_plain() call for this purpose, by the way. I don't think it's the cause of the problem, though, unless the value you are actually checking for has HTML special characters in it. Commented Mar 19, 2013 at 17:01

1 Answer 1

2

There's no syntax error in the second code sample, if you're getting a syntax error it's not coming from those lines.

The best way to get values from an entity is with field_get_items():

$items = field_get_items('node', $node, 'field_room');
if (count($items) && check_plain($items[0]['value']) == 'Value1') {
  // Do something
}

But unless you have some sort of multi-lingual issue the code you've got should do exactly the same thing as that, so the problem might be elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.