Theming the node "Body" field

By Stephen Musgrave
Jul 27, 2009
If you've ever tried to theme the "Body" field after adding at least one CCK field, then you know my pain – there is no way to reference the body field through CSS – there is no element to hook into.

This morning, I went out looking for a solution and found that someone had contributed a module in the Drupal issue queue.  I downloaded the module and investigated the contents and – of course – it's so simple that I'm felt a bit sheepish for not having discovered this solution earlier.  The module simply wraps a <div class"node-body"></div> around the body field.

There were some problems with the module code in the ZIP file that was initially uploaded as it didn't declare a Drupal version and there were some syntax issues in the module file.  After a minute of crossing t's and dotting i's, I came up with this:

<?php
function bodywrapper_nodeapi(&$node, $op, $teaser, $page) {
  if ($op == 'view') {
    $node->content['body']['#value'] = "<div class=\"node-body\">" . 
      $node->content['body']['#value'] . "</div>";
  }
}

Simple, isn't it?

Comments

Nice find. It's funny what a

Nice find. It's funny what a basic, obvious, totally overlooked feature this is in Drupal. I hope they implement it in 7...

With "Fields in Core", I

With "Fields in Core", I would certainly hope that Title and Body are seen as CCK fields. While the DB structure still might have them as old-school node fields, I would hope that they are as feature rich and malleable as CCK fields.

Beautiful

This works perfectly! Thank you.

It's kind of ridiculous considering the otherwise div overkill mantra of Drupal, that this hugely important div would be left out.

Too bad it requires a full custom module. Would be great if there was a way to do this at the theme level.

After hours of searching, it works!

How simple...
But I'm wondering, could this be done in a .tpl.php file or by using a function in the template.php file?

You could, but then you get

You could, but then you get into the problem of having to explicitly echo out all your fields fore each content type you create -- you would need a separate tpl file for each node type -- because the CCK fields and the Body field would all be merged into the content field in the node array. Depending on your themeing, you may want to do that anyway, but this module handles most cases for me.

Doesn't work

So I do. tpl.php file didn't work. oyun indir

.tpl.php

Hi thanks for your efforts, and sorry for repeating the question: can it also be done with a template like "node-body.tpl.php"?
or function in template.php file?

Good question. Did you try

Good question. Did you try it?

1) Thanks! 2) Could you please publish it as contributed module?

TEST...

Thanks! Your module perfectly solved my support inquiry: $content holds node body as HTML elements in loose sequence not wrapped within DIV container (to easily separate ImageField DIV)

I am asking you for only one further minor favor: Could you publish this module into the official contributed section at http://drupal.org/module/* preferential under your already chosen name "bodywrapper"?

This would improve the situation for people who:
a) Search for a problem solution within the Drupal module section, and not through external search engines.
b) Me/others could easily/reliably post a Drupal internal link to related issues.
c) People which install through automated means such as Plugin Manager.

I already noted down about 5 related issues, and as soon as http://drupal.org/module/bodywrapper would become available, I would love to post into this threads to help the subscribed people and future troubleshooters.

Thanks for the encouragement

Thanks for the encouragement -- I certainly should contribute this module. A mix between being time-strapped and the horrors I hear about getting the process of getting setup for a module contribution have kept me at bay so far.

If you have an account already setup and want to take this on, I'd be more than happy to support that.

So far I only contributed

So far I only contributed German translation strings, but I never contributed a module. I also know nothing about versioning control systems and the like. And I have currently no motivation to go into this. Sorry, for this I have to politely play the ball back to others.

What I would certainly do if this module gets published is to promote it as a solution in many of the support issues which are related to the problem. That would be my contribution.

Meanwhile I realized that Gabriel Radic was the module originator of Body Wrapper, and you then offered a slight modification. Correct? I have approached Gabriel Radic whether he can officially publish the module. And as he already contributed modules, and is a very early and likely experienced Drupal user ( #3951 ! ) , chances seem to be good. So let's wait for his response.

nice catch

Thanks for this tip and module, was searching for this...

body node

Just what I was looking for to finish a project. Thanks for the info.

    Drupal Association Organisation Member     N-TEN Member
X
You may login with either your assigned username or your e-mail address.
The password field is case sensitive.

Loading