I have a simple custom node with a text field (see attached screen capture).
Only the admin user is allowed to create and edit this node type but I assume
that authenticated and anonymous users can view it.
If I try to output it in the node-content_sushi_store.tpl.php file using this:
print content_format('field_store_body', $node->field_store_body[0]);
It works perfectly for the admin user, but anonymous and authenticated users
only see n/a when they go that node page.
If I try to output it in the node-content_sushi_store.tpl.php file using this:
print $node->field_store_body[0]['view'];
then it works as expected. However, I'm told that using content_format() is
the "safe" way to output the field.
| Comment | File | Size | Author |
|---|---|---|---|
| list_2.png | 17.07 KB | slimandslam |
Comments
Comment #1
yched commentedis the right way.
The value in 'view' already went through content_format().
Comment #2
scoutr commentedIf you need to use the above code to reference a view in an array:
Comment #3
TimK commentedThe solution is to use the following instead:
(Note the additional two arguments passed to content_format.)
Explanation:
The problem is that content_format, at least for text fields with the 'text_processing' option, invokes check_markup to format the field according to the selected content filter. OK. That's as expected.
However, it also must check whether the current user is allowed to see the content. If the node is being previewed, the text_field_formatter tells check_markup to test whether the user is allowed to use the selected content filter. This makes sense, because if you're previewing a node, then you're still developing the content, and you may need privilege to use certain content filters, such as the PHP filter. If the node is not in preview (i.e., if it's published), text_field_formatter tells check_markup to show the content to everybody, regardless of whether they're allowed to author content with the specified content filter.
Now, if there is no node (i.e., $node = NULL), text_field_formatter assumes the former, which means that only users authorized to use the content filter will be able to view the field. So passing the $node into content_format allows text_field_formatter to check the in_preview flag, thus solving the problem.