I'm working on themeing a node and I have come across an issue with $node->body. It seems that Drupal renders the $body variable to include all the fields in the node, not just the $body field. So there's no way to remove unwanted fields from the $body (at least not that I'm aware of...).

I know I can use <?php print $node->field['0']['#value'] ?> to output individual fields but I'm using the Inline Module, and when I output individual fields, my inline images don't show up. The inline images only show up when I use <?php print $node->body ?> because the module replaces the inline image token during the processing of the body field. So it seems like the best way to get the $body field by itself, with the processed inline images is to just remove the unwanted fields from the $body variable...

Is there a way to do this? Or some sort of workaround? Can I somehow override or specify the fields which are being rendered as $node->body?

I realize this is kind of a complex question so I really appreciate any thoughts anyone can offer!

Cheers!

Comments

yuriy.babenko’s picture

You can configure the visibility of individual fields.

Administration -> Content Types -> Edit content type -> Display Fields tab -> Set field to hidden

Usually something like yoursite.com/admin/content/types/story/display

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

tflmike’s picture

This does remove my cck fields from the $node->body... but it also doesn't allow me to bring back those cck fields in my node-contenttype.tpl.php file. I need to be able to have a filtered $node->body, wrapped in a div and separated from the other cck fields.

I had no idea it would be this difficult to do! This seems to be a major issue with the way Drupal renders nodes!

This is my fourth day trying to figure this one thing out... if anyone's got any other suggestions for me I'd really appreciate the help!

yuriy.babenko’s picture

OK, then use: print $node->content['body']['#value'];

Oh, and for future reference... when in doubt:

echo '<pre>';
print_r($node);
echo '</pre>';

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

tflmike’s picture

Yes... I've used print too.

The problem with using <?php print $node->content['body']['#value']; ?> is that it prints out the raw text of the body field. So my token to insert my inline images break – because the inline module uses tokens which are replaced by an input filter – and I lose all my formatting. That's why I've been trying to see how I can separate the cck fields from the $node->body, because the node body is processed by the input filters and my inline image works.

So here's what I've tried so far:
1. print $node->body and print $node->content
Result: Prints out the processed $body field. Inline images show up but theming fields is impossible because they are "rendered" by Drupal and output as plain html.

2. print $node->content['body']['#value']:
Result: Prints out the individual $body field but the output is raw (hasn't been passed through the input filters) so inline images don't work.

3. Creating a custom CCK body field:
Result: the fields can be themed individually, but again the output is raw so inline images don't work.

4. I've researched drupal_render(), node_view() and nodeapi() but unfortunately I don't understand enough about drupal core to determine how I can use these or possibly override them to get the result I want.

5. Writing my own module: I tried this 3 times for about an hour each time before realizing I have no clue what I'm doing.

6. I've chatted with the maintainer of the inline module to see if he had a suggestion. Apparently there might be some features which will help this issue in InlineAPI (the next version) but who knows when that will come out.

I'm wondering if this might be a major drawback of Drupal 5 (the ability to truly customize nodes)... Does anyone know if they have adressed this in Drupal 6?

Thanks!

EyeChartBrew’s picture

tflmike: I was having this very same "contemplate + CCK fields + Node Body" migraine on my Drupal 6.3 site. That is, until I saw this thread.

FWIW, print $node->content['body']['#value']; works like a champ.

Good luck, and cheers,
//TB

dman’s picture

In D5 at least, the value of $node->content[*]['#value']; certainly is the 'cooked' ready-to-render HTML that has been passed through the input filters. You should be able to experiment with different input formats and see the difference in that array. Your version #2 above works for me (on the php input format at least).
I'm not sure what's been going wrong for you if that's not happening as expected, or maybe it's something peculiar to the inline module.

If that's just not working, you really want to take over rendering and apply the input filter yourself, you may try check_markup($text, $format) over the text you want, although when I do so I get the filter applied twice.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

ladamiak’s picture

I've tried these steps too and still I have the same problem: I can't say 100% sure that $node->content['body']['#value'] will print the filtered value.

Actualy I guess it would be better if we could build the "view" for a specific cck field. Something like field_view($node,$field,"teaser|page"); that would output something like:

[field_fotos_story] => Array
    (
        [0] => Array
            (
                [fid] => 43
                [list] => 1
                [data] => Array
                    (
                        [alt] => 
                        [title] => 
                        [process] => 1
                    )

                [uid] => 1
                [filename] => 43.JPG
                [filepath] => sites/default/files/imagens_story/74/43.JPG
                [filemime] => image/jpeg
                [filesize] => 1263691
                [status] => 1
                [timestamp] => 1231365343
                [nid] => 74
                [view] => THE_RENDERED_VIEW_HERE
            )

    )

If anyone knows how to do this in the theme, please, let us know.

drclaw’s picture

Hi,

I know this is about a year and a half late, but I was struggling with the same issue and found a solution that works pretty well. If you use the print_r() function and enclose the output in a "pre" tag, all the line breaks and spaces will be preserved. In other words, in your .tpl.php file, just put:

<pre> <?php print_r($node) ?> </pre>

And I believe you will get a much cleaner output of all the properties and arrays stored in $node.