Drupal tip: Stripping out nested divs in views blocks

Moshe and merlinofchaos for pointed me in the right direction for the function to modify if you want to remove divs that are output in a list view block via the Views module. To strip out those divs, do something like below in your PHP Template's template.php file:

function phptemplate_views_view_list($view, $nodes, $type) {
  $fields = _views_get_fields();
  foreach ($nodes as $node) {
    $item = '';
    foreach ($view->field as $field) {
      if ($field['label']) {
        $item .= '<div class="view-label view-label-$field[queryname]">'. $field['label'] ."</div>";
      }
      $item .= views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "<br />";
    }
    $items[] = "$item"; // l($node->title, "node/$node->nid");
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

Thanks to everyone for the suggestions.

Comments

01 Matt Dawson
03/07/07 @ 17:20

If I can actually get this to work, I'll owe you a beer. That is, if you drink.

So you're saying this function should be inserted into the theme's template.php file, correct? Should it work without alteration in most themes? I understand PHP enough to get the basics of what it's doing, but I can't figure out what I might need to change to make this work on my site.

I should mention: my homepage is WordPress, not Drupal - I only have a very VERY early stage Drupal site up for testing purposes. The only view block I have set up is a "Top Nav" list here: http://thenestedtest.com/drupal The site couldn't be uglier - I've decided to use the Sympal theme to develop on, simply because it ouputs the cleanest, most minimal HTML. The site I'm ultimately porting to drupal is here: http://southernenvironment.org

Do you have ANY idea what I might need to change to get the function you've authored to work?

Thanks a million for posting this!

02 Matt Dawson
03/07/07 @ 17:35

Oh, crud.

I just realized I may have made an @ss out of myself. The block I'm trying to strip divs from is a menu block. Huh.

Would it be terrible if I created a block that wasn't a menu and used it as a nav bar, would that be a bad idea?

Nevermind me if I'm talking gibberish...I think I'm a good six months away from understanding Drupal.

Advertisement
03 jibbajabba
03/07/07 @ 18:10

I can't imagine that you're using the Views module to generate that list. The code above is to strip out the extra divs that the Views module puts out. I'm not understanding what's going on for you.

04 Alexei A. Korolev
07/25/07 @ 09:12

Good tip. I need to try it to remove div tags in my project. It's really PIA for me now :S

05 Klaus
08/14/07 @ 06:48

Sorry, how to use this tip in WordPress?

06 jibbajabba
08/14/07 @ 09:55

This is a tip related to Drupal Views module.

07 fonts
08/20/07 @ 05:25

Thank you for mentioning Sympal theme. The most themes has terrible html code...

08 unempoyment quit job
09/10/07 @ 00:50

you need to update your Drupal, even for testing purposes, as you are not getting a good indication with results from that early of a release

09 Trojan Horse
12/11/07 @ 02:51

Yes Sympal is quite the theme. Doesn't have all the computer security problems that the others do...

10 Charity cds
01/20/08 @ 07:54

And I thought lerning Italian was hard - man there's so much to learn about Drupal...

Advertisement
11 still life artist
01/27/08 @ 10:18

The most themes has terrible html code...
andrzej
http://www.andrzejfilipowicz.com

12 metheav
01/27/08 @ 19:54

Thank you for mentioning Sympal theme.

gta vice city

13 hcp
02/21/08 @ 15:45

Charity cds: ofcourse learning something you have ever known is hard at the beginning.

14 gausarts
03/01/08 @ 18:46

I created 10 to 15 different block views and all use the generated codes by views theme wizard which produced function phptemplate_views_view_list_VIEWNAMEs with all the same codes but for the VIEWNAMEs . Putting all those similar codes into my template.php seems to be irrational.

Does anyone here in this resourceful site know how to combine those functions so that I can at least eliminate code duplications?

Any help would be appreciated. Thanks.

15 Yahoo webmaster
07/08/08 @ 02:05

Thank you for post.

16 Drupal Theme Garden
07/23/08 @ 06:01

Nice tip.

Question:
is that code created for D5 or D6 theme (or both)?

17 jibbajabba
07/23/08 @ 13:39

That's for Drupal 5. Not tested in 6.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <strong> <dd> <dl> <dt> <i> <li> <ol> <u> <ul> <code> <blockquote> <fieldset> <legend> <h6> <span> <img> <div> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options