2

I'm wondering if there's a way to template a View, and actually be able to manipulate the content within it. I know I can theme the 'wrapper' around the View, but I'd like to be able to override what's inside the View, so I can create my own loops et cetera.

Right now I'm just having to create a view, and then make a template for the block (block--views-block--view-name.html.twig for example). This doesn't give me any access to the fields within the view though. I just have to wrap my markup around a {{ content }} tag, and adjust the formatting settings of that view.

I believe this functionality was available in D7, can I do it in D8?

EDIT: Another example I just thought of is if I had a content type for locations of offices, I then made a view to display a list of these locations to show on a page. I want to be able to have access to data stores inside each location. This way I can then create loops etc on my contact page, looking through each location, displaying it how I'd like.

  • but I'd like to be able to override what's inside the View, so I can create my own loops et cetera. you need to be more specific. – No Sssweat Jun 27 '16 at 10:23
  • Well, as an example, I have a view that shows the latest 4 blog articles. Blog articles is a content type. I'd like to be able to access all the fields of the content type for each node. – Daniel Dewhurst Jun 27 '16 at 10:30
1

Well, as an example, I have a view that shows the latest 4 blog articles. Blog articles is a content type. I'd like to be able to access all the fields of the content type for each node.

You need to add all those fields to your view, then you can use views-view-fields--[viewname]--[block-1].html.twig

and use {{ fields.title.content }} or {{ fields.field_name.content }} to print the fields on your twig template.

  • How do I get an image's URL? I can't kint out fields or fields.featured_image. – Daniel Dewhurst Jun 27 '16 at 13:15
  • 1
    I eventually got the URL from my image field by adding the Image URL Formatter module. – Daniel Dewhurst Jun 27 '16 at 14:18
3

You should create new display modes for the content in question, and provide twig templates for them (e.g. node--type--display-mode; I am unsure of the pattern). Then you can change the View to show the rendered entity, and select that display mode, which will use the twig template for it.

This will be a lot easier IMO for what you are trying to do and make the View itself more maintainable long term. For example, maybe you wind up having to make multiple views... editing all those fields via Views is going to become a real pain real fast. Changing the field configuration in the display mode settings of the respective entity type will be reflected in the View immediately without changing the View itself (unless caching is enabled, then clear it).

See: http://www.mediacurrent.com/blog/drupal-8-theming-without-panels for some background.

1

To achieve this create unformatted view. Then create custom template for that view. Give name to that template as views-view-fields--viewname--page/block.html.twig and then create the HTML structure and then simply print the field accordingly and then save the file and don't forget to flush the cache.
To print the field...suppose you want to print the title and image field then do as below {{ fields.title.content}} {{fields.machine_name_of_image_field.content}}
thats it.

Note: To get the machine name of the field, goto the content type and then manage fields, there you will find machine name of those fields. And remember if you have not add field in view in admin panel and adding it in your template file then it will not reflect.

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.