3

The output of a view I am using contains the following code.

<li class="views-row views-row-1 views-row-odd views-row-first">  
  <div class="views-field-field-cimg-fid">
    <span class="field-content">....</span>
  </div>

  <div class="views-field-field-yma-value">
    <span class="field-content">...</span>
  </div>

  <div class="views-field-field-price-value">
    <span class="field-content">....</span>
  </div>

  <div class="views-field-changed">
    <span class="field-content">....</div></span>
  </div>
</li>

I want to add <div id="test">....</div> around the following snippet.to group two fields

   <div id="test">
    <div class="views-field-field-yma-value">
      <span class="field-content">...</span>
    </div>

    <div class="views-field-field-price-value">
      <span class="field-content">....</span>
    </div>
      </div>

the loop

 <?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>

how to add the div tags to the loop?

2 Answers 2

6

In the View edit screen, click Advanced. At the very bottom of that, you'll find a bit called: Theme: Information. Click the Information link and a new window will pop up, showing you theme overrides for the view. Each category changes different parts of the view.

All of the file names listed on each type will show the available filenames which could be used to override this view, in order of least specific to most specific. So if you pick the first one, it will probably override all of your views, while if you pick the last one, it will only override that view in a certain display.

Once you've picked what you're going to override, click the link and copy all of the code shown there to the clipboard. Then go to your theme directory and make a file with the filename you picked from before and paste in the code you copied. Now just add the <div> tags you want wherever you like.

You'll need to click the "Rescan template files" button for Drupal to pick up your new theme override file. Views shows you which one it will use for the view by putting the one it will use in bold.

Edit: When writing this, I was going through the steps for Drupal 7, but I believe the steps are very similar for Drupal 6.

Next Edit: From the code you have pasted, it looks like the most appropriate place to put the new div tags would be (see the asterisks):

**<div id="my-custom-div">**
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
  <?php if ($field->label): ?>
    <label class="views-label-<?php print $field->class; ?>">
      <?php print $field->label; ?>:
    </label>
  <?php endif; ?>
  <?php
    // $field->element_type is either SPAN or DIV depending upon whether or not
    // the field is a 'block' element type or 'inline' element type.
  ?>

    <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
    </<?php print $field->inline_html;?>>
<?php endforeach; ?>
**</div>**

I normally experiment a bit with this, so it's a bit difficult to tell you the exact spot offhand, but hopefully the above will be close...

2
  • Its a Views version difference, not Drupal. The difference is that in Views 2, the "Theme" part is exposed automatically, you don't have to go to Advanced Settings like in Views 3.
    – Alex Weber
    Apr 12, 2011 at 19:25
  • i am still don't know how to add the div to the foreach loop?
    – enjoylife
    Apr 13, 2011 at 6:14
0

@Frederik's answer didn't work for me. If you just want to wrap a div around certain fields in your view you have to build some logic in. I think that code will just wrap an additional div around the entire view loop. This worked for me:

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>
  <?php if ($field->class=="title"):?>
    <div class="extra-savoir-faire">
  <?php endif; ?>
  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
  <?php if ($field->class=="field-space"):?>
    </div>
  <?php endif; ?>
<?php endforeach; ?>

The key was using the $field->class as an identifying thing to check against per field. So this allowed me to throw a div before the Title and after the "field_space" view field. I saved this as the most specific tpl.php file listed in my theme information for "Row style output" using views-view-fields.tpl.php as a starting base.

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.