Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am wondering am I doing the right "Drupal" way in getting formatted value of node's body field. Here is my code:

$field = field_get_items('node', $node, 'body');
$body = '';
if (isset($field[0]['value'])) {
  $body = check_markup($field[0]['value'], $field[0]['format']);
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The API has the field_view_value() function which essentially does the same thing as your code, except it applies all of the field/instance settings to the value so you won't need to make changes to your code if you change a field setting.

You can use it like this:

$field = field_get_items('node', $node, 'body');

$body = '';
if (isset($field[0]['value'])) {
  $body = field_view_value('node', $node, 'body', $field[0]);
}

Just in case you want the default HTML wrappers included with the field output, you can use field_view_field() instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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