I use views module in my website (built in drupal-7). I have a custom theme. I have set a views-view-fields--XX.tpl.php like follows.

    <?php /**`
 * Default simple view template to all the fields as a row.
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->wrapper_prefix: A complete wrapper containing the inline_html to use.
 *   - $field->wrapper_suffix: The closing tag for the wrapper.
 *   - $field->separator: an optional separator that may appear before a field.
 *   - $field->label: The wrap label text to use.
 *   - $field->label_html: The full HTML of the label to use including
 *     configured element type.
 * - $row: The raw result object from the query, with all data it fetched.
 * @ingroup views_templates

<?php foreach ($fields as $id => $field): ?>

<?php if ($id == 'field_voti'):?>
<div class="uniscicampischeda">
<?php endif; ?>
    <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
    <?php endif; ?>
    <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
    <?php print $field->wrapper_suffix; ?>
    <?php if ($id == 'distance'): ?>
<?php endif; ?>
<?php endforeach; ?>`

in order to insert some fields into a new div with a specific class.

With firebug I see the following:

<td class="col-1 col-first vistacomplessiva">
    <div class="views-field views-field-title nomeattivita">..</div>
    <span class="views-field views-field-name">..</span>
    <span class="views-field views-field-last-updated">..</span>
    <div class="views-field views-field-field-foto">..</div>
    <div class="views-field views-field-body descrizione">..</div>
    <div class="uniscicampischeda">..</div>

All that I want to do now is to have the div with class="uniscicampischeda" before the div with class="views-field views-field-field-foto", so that with css I will give the right attributes (I want class "uniscicampischeda" with float=right attribute) HOw can I do that? do I have to write some code into views-view-fields--XX.tpl.php file? How? Can anybody help me? Thanks.

4 Answers 4


Well, there are three solutions for your problem,

  1. Forget about the div with class uniscicampischeda and try to work with the existing classes in order to align them to right.

  2. If you want to insert the div.uniscicampischeda, try to find out which one of the PHP lines generates the <div class="views-field views-field-field-foto">..</div>, then edit the .tpl file and put the DIV before that PHP line.

  3. As the last solution and also the most complicated one, in your template.php create function hook_preprocess_views_view_fields(&$var), in this function get the Views fields value and insert them in to the $var array, then in the .tpl file use those keys of the $var array as the variable that generates the corresponding value. Using this solution you will have much more freedom to theme your view output.

If you need help about any of these solutions just let me on it.

  • +1 I would go for option number 1. No need to make simple things more complex. I'm sure the existing classes are enough to accomplish the job unless I am missing something.
    – Gulok
    Commented Aug 10, 2015 at 1:41
  • @Gulok that's right
    – M a m a D
    Commented Aug 10, 2015 at 3:54

I think what you want to do is make a separate views-view-field--FIELDNAME.tpl.php to theme your individual field with that div class. Then in the template you have for the fields, you can iterate through the fields. Is there any reason not to re-order your fields in the view itself?


You can do it easily from back end. There is no need to add it in the .tpl files

Just add a new field in view and filter from above dropdown. Now choose 'Global Custom text' mentioned in the image. Now put your divs in the mentioned textarea.

Hope it will help!

enter image description here


try edit your views-view-field--XX.tpl.php not FIELDS, also double check your iteration end starting and closing tags for the div.

Also take note that firebug will auTO-CLOSE an unclosed HTML tag so check the :view-source in order to be sure whats rendered in HTML...

  • I thank you for your answer, but I don't understand what you mean...the element I want to place is a div with differents fields that I want to move before another field. Why do you suggest me to edit views-view-field--XX.tpl.php ?
    – user11609
    Commented Mar 4, 2013 at 16:08

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.