2

I want to render the rows of my “Recent content” Views page using a template in order to

  • subdivide the rows into multiple divs,
  • freely move around the rendered fields within these divs, and
  • apply some custom PHP where needed.

Idea A (fails): I could achieve this by creating a new Display Suite layout for nodes and using this layout to render my view set to display format “Content”. However, I need to group the rows by a specific field, and grouping by fields is not available in the “Content” display format – for this, I would need to chose the display format “Fields”.

Idea B (fails): When displaying fields (say, as an unformatted list), I can make use a copy of

  • views-view.tpl.php to adjust the coarse layout of the view, e.g., put an additional div around the whole content block;
  • views-view-fields.tpl.php to adjust how a single field should be rendered;
  • views-view-unformatted.tpl.php to adjust the coarse layout of rows, e.g. put an additional div around the whole row. However, here the $row variable is not an array of rendered fields but rather a string containing the full html markup of the row, so I cannot access single fields and move them around within the row as I wish.

Idea C (works, but isn't it extremely inefficient?): I could display an unformatted list of fields, but render only the Node-ID-field. Then, in views-view-unformatted.tpl.php I can load the whole node corresponding to the current row via $node = node_load($row); and access all fields attached to it via $node->field_whatever. These I can then move around as I wish, wrap in divs, and apply custom PHP.

Idea C seems to work, but it also seems highly inefficient, as I will have to load the full node for every row. Also, I want to leverage the many views display formatters that my fields come equipped with, which I would have to re-build manually.

Question: Is there a way to specify formats for specific fields within the Views UI, submit only these to a template (and not the whole node), and move them around freely within the row as explained above?

0

I had a similar issue recently and this is what I did.

Let's say for the sake of argument that we have 6 fields: A to F. I wanted to wrap A-C in a layer eg "col-1" and D-F in a second layer eg "col-2". I overrided the field templates (views-view-fields--field-*.tpl.php) for A, C, D, F as such:

  • Added a <div class="col-1"> element in A, without a matching closing tag </div>.
  • Added a <div class="col-2"> element in D, without a matching closing tag </div>.
  • Added the respective closing tags </div> i n C and F.

This may show some error or warning in your IDE, but the rendered HTML should be ok as long as the order of the fields in the views allows it (eg don't re-arrange D to be above C).

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.