I have bad-designed a view, and the result is that I have the following scheme:

  • Field 1: Category (taxonomy)
  • Field 2: Node title
  • Field 3: Image
  • Field 4: Other data (PHP processed).

The problem is that I need field 1 to appear just once for a group of nodes and not always. As I had to redo all the View, I have made the following process, please tell me if there is a better solution.

Instead of not displaying field 1, grouping by that, etc., as I have some PHP process on it, I do the following:

In views-view-fields.tpl.php I identify the category, and prepend [[BEGIN]]. I identify the last field and append [[END]].

In views-view-unformated.tpl.php I replace [[BEGIN]] and [[END]] with <div> wrappers; depending on the valued inside (preg_match), I make the category title appear just once at the beginning of the list of products.

Please tell me if that is a valid approach (it works) or it should be more desirable to re-draw the view (hours of changing fields, logic, etc.).

The string replacement outputs an additional <div> which wraps all the complete row of fields. This I had in the view as the field 1 was just another field and did not encapsulate anything. Maybe even if I would set it as grouping field it didn't encapsulate anything. The result I want (and get) is:

<div class="other-class" id="category-119"> divs and HTML stuff from the view  </div> 

The only easy and fast way of doing a <div> encapsulation of the row is using those delimiters in the template files.

  • Can you also post your expected output? – iamroald Aug 19 '16 at 6:36
  • Added at the end of the edited answer. Basically I get a warping div with some codification that I can not get by any other means, but surely there must be a (far) better way of doing this. – Cesar Aug 19 '16 at 6:41
  • do you want to wrap the div in all views row or each rows? just want to clarify before answering it. – iamroald Aug 19 '16 at 6:57
  • All views rows til the end is marked. In fact, I put just a unique delimited [BEGIN] as it does not always appear (category is not shown, php logic). When I find [BEGIN] I wrap inside an additional div. By using counters I take care of the closing div tags. – Cesar Aug 19 '16 at 7:13
  • You want field 1 to appear in row 1 only? Is that your question? – No Sssweat Aug 19 '16 at 7:15
  1. Add field and Exclude from Display.
  2. Add Global: Text area in the header.
  3. Check Use replacement tokens from the first row
  4. Enter the row pattern
  5. Use css to make it look part of the first row

That will output the first row's field in the header.

| improve this answer | |
  • I don't see how the Header will be repeated after a few rows, when the Category changes, it seems something that goes just once at the very beginning of the View. Also, I have used the replacement tokens, and get "Content: Category" literally written when I use [field_category]. I would have expected the full pattern text be replaced by the category name, and not by the literal "Content: Category". – Cesar Aug 22 '16 at 5:26
  • Ah ok, I thought you only mentioned grouping as a way to give the field.. Didn't realise there were multiple types. – Niall Murphy Aug 22 '16 at 6:04
  • And what about the literal "Content: Category" being shown? What is its value? May it be a bug? – Cesar Aug 22 '16 at 6:06
  • What happens with [title] or [nid] ? – Niall Murphy Aug 22 '16 at 6:25
  • Title is the only one that behaves as expected (see replacement by Cold fusion of [title]: [Content: Category]">[Content: Category] Title: Cold fusion NID: [nid] – Cesar Aug 22 '16 at 7:32

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.