2
\$\begingroup\$

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']);
}
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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