1

Ok... here goes. I've had a lot of trouble trying to track down any sort of example like this, and the Drupal 7 Views documentation doesn't even document the views-view-field.tpl.php.

I'm trying to override the default display for a field inside of a view because the default field html content is not at all sufficient to my needs. I can almost do it, except that the views module likes to inexplicably wrap the field content in a that I cannot override through the templates. Also I'm forced to hack into the field's $row variable internals directly, which does not strike me as a good long term strategy.

views-view-fields--slider.tpl.php

<div class="slider-item">
  <a href="<?php echo $fields['field_slide_link']->content; ?>">
    <img src="<?php echo $fields['field_slide_image']->content; ?>">
  </a>
</div>

views-view-field--field-slide-image.tpl.php

<?php echo image_style_url('slide', $row->field_field_slide_image[0]['raw']['uri']); ?>

views-view-field--field-slide-link.tpl.php

<?php echo $row->_field_data['nid']['entity']->field_slide_link['und'][0]['safe_value']; ?>

So to summarize the two problems:

  1. Diddling with the $row internals directly makes me quite uncomfortable. Surely there's a better and safer way of getting the not quite raw field values that I care about.
  2. The rendered field content is always wrapped in a div.field-contain and I cannot eliminate it through the template, as I would have expected. I'm loath to call strip() to remove the html (I can think of several use cases where I might not want that wrapper).
1

This doesn't directly answer your two questions but should help...

The Views documentation on template overrides doesn't need to be particularly good...as those overrides are self documenting.

Open up any View for edit, open the Advanced fieldset, and click "Theme: Information" at the bottom.

You'll get something like this:

enter image description here

A complete list of every template file available for every field, and the view itself. From there you should be able to experiment quite quickly to find the template name you're looking for.

3
  • This is a handy tip but unfortunately, as you say, does not answer either question. The problem is not which templates to override, but rather how to access the variables in that last views-view-field.tpl.php safely, and how to get rid of the div.field-contain that Views automatically wraps around that template's output (e.g. it seems to be coming from outside the templating system itself... maybe one of the pre-render/post-render calls I haven't figured out). Aug 8 '12 at 21:32
  • I think that comes from the one of the style output templates but I'm not totally sure
    – Clive
    Aug 8 '12 at 21:49
  • Actually it doesn't. It seems to be wrapped by the module in between the views-view--field and views-view--fields templates... (there is no templating layer between these). Aug 10 '12 at 17:18
0

I never was able to remove the stray div.field-contain.

To accomplish the same sort of image linking I wanted I was able to specify a re-write rule on the field-slider-image field inside of the View configuration. You set the rewrite to be a link and then have the link be

[field-slide-link]

This prints out the image wrapped in the appropriate link, with all the specified attributes.

Rewriting Drupal Views Output Custom Theming CSS

This completely elliminated my need to hack on views-view-field--field-image-link.tpl.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.