1

I have a db_select() that returns the *body_value* and *body_format* from the table field_data_body...how to I properly render that in Drupal?

1 Answer 1

5

I think the 'proper' way to do this is not to get the data directly from the database at all, but to use the built-in field API:

// Get an entity (node in this case).
$node = node_load(1);

// Get the view for the body field
$view = field_view_field('node', $node, 'body');

Doing it this way you won't have to bother negotiating languages or anything that, and the field will be outputted exactly as it is when it's attached to a node display.

If you do want to do it manually, though, you need to use the check_markup() function:

$text = check_markup($body_value, $body_format);

It might be worth taking note of the comments in that function, you need to be careful about accepting an arbitrary filter format without validating it:

Note: Because filters can inject JavaScript or execute PHP code, security is vital here. When a user supplies a text format, you should validate it using filter_access() before accepting/using it. This is normally done in the validation stage of the Form API. You should for example never make a preview of content in a disallowed format.

3
  • So much overhead though :s I swear there was a way to call render() or something similar to format field data... Jun 1, 2012 at 15:28
  • Yeah there must be a way but I can't think of it off the top of my head...will have a scout through the filter module and let you know
    – Clive
    Jun 1, 2012 at 15:32
  • Ah turns out it's fairly easy, answer updated :)
    – Clive
    Jun 1, 2012 at 15:36

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.