2

Newbie here!

I have created a view with some fields and i want to group some of the fields in one wrapper and remaining in another wrapper and also want to change the structure i.e. adding own wrappers and classes.

Thanks.

4 Answers 4

7

To add and then group fields in views, you can do the following (if not using a view mode/template).

  1. Add all the fields you need, and mark each of them "Exclude from display". Exclude from display

  2. Add another field of type 'Custom text'. DO NOT exclude this one. enter image description here

  3. Click on the 'Custom text' field to open its options.

  4. Inside the textarea there, you can input custom html using the tokens from the fields above to populate it. You can find the values you'll need for replacement if you click on the "Replacement patterns" dropdown just below the textarea. enter image description here

3

You don't need templates for grouping fields in views. If you have other question, please provide more details.

Example:

Add the fields Title and Changed, hide Title by checking "Exclude from display". Go in the last field of the group Changed, open REWRITE RESULTS, check "Override the output of this field with custom text" and put in the text box:

<div class="myclass">{{ title }} was changed at {{ changed }}</div>

This is the result:

Title was changed at 08/18/2016 - 13:50

3
  • i know i don't need a template for grouping.I was asking about how to override a view template as well as group the fields in view @4k4 Thanks Commented Aug 18, 2016 at 11:59
  • Grouping was the only specific question you asked. Theming of views in general is too broad for an answer. Did you try twig debug?
    – 4uk4
    Commented Aug 18, 2016 at 12:05
  • I follow the same but It is not displaying the values of specified fields @4k4 Commented Aug 19, 2016 at 5:36
2

If you want to use a twig template instead of rewriting the results in a view, you just need to:

  1. Figure out what template you need to override by using the debug setting in settings.php and looking at the HTML code for the template name.
  2. Create a copy of the template in your custom theme, and perhaps rename it according to the template naming conventions, for example views-view-fields--our_team--page_1.html.twig, where our_team is the machine name of the view and page_1 is the display machine name.
  3. Edit the template as needed. You may want to use Devel and kint() to figure out what variables you have to work with. One example might be {{ fields.field_staff_photo.content }} but that will change depending on the names of your fields.

Using the rewrite results option in a view is probably quicker and clearer to work with, but if you want to keep all your markup in files, that could be a good reason to go with a template.

1

If anyone is just looking to group results from a Drupal view by a field, here are answers from a similar thread. I often use the top answer there when I have categorical data to organize.

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.