A lot of hours without sleep behind me.. so I need to beg for some help with views-view-field.html.twig.

What I need: I need to add some background style with field image for slider I am creating with views. Each slide have different image in background. It is forbidden to use style in field, so I am working with single field template. I need to display hidden field from views in field template. In documentation I have something like this

Available variables:
 * - view: The view that the field belongs to.
 * - field: The field handler that can process the input.
 * - row: The raw result of the database query that generated this field.
 * - output: The processed output that will normally be used.
 *
 * When fetching output from the row this construct should be used:
 * data = row[field.field_alias]

But when I trying to display this field - I fail every time. I used a lot of variations for display, but don't understand how to use this fetching output construct for hidden field in views. There is a lot of topics about if for Drupal 7, not working for me in drupal 8 and twig. Thank you for any suggestion.

Comments

JacekM’s picture

Let me be more specific:

I've created view (block displaying conten type with HTML list) I've set up few fields like title, motto, pictures, etc. and excluded them from view. Last one field is picture for background of li element of slider. In this field I write rewrite code with twig and all hidden fields for display slide in different way. This one field is not excluded from view, but have rewrite results. I choosed this way to have access to this field itsfelf, and bound all stuf inside with div background url(thisfieldimage). I can't get acces and print url for this image at all.

JacekM’s picture

Ok, so how I solved it... Maybe there is other, better solution?

  • I rearrangement my fields in view: first with content, title, pictures and links,
  • I've added one more custom text field with rewrite - then I put in this rewrtite all fields above with some twig function to display this in different ways and hide this field to.
  • Then I've added last field, field_background which is one with slide background picture. I turn it to display just URL of picture and write rewrite inside to display:

    {{ field_background }} )"> {{ nothing }}

where nothing is my field with all content stuf inside.

I've edited twig template for last field and write something like this:

<div style="background-image: url( {{ output }}
so when views start rendering content it create code from my last field which is wrapprer with background image around content form other fields above.

Well, it's work, my slides run with background picture. But I dont fell good with that, I think there is better way to get it in one field.

andrey7cr’s picture

Hello JacekM I'm trying to display a specific HTML markup, but I can't even print a field in views-view-fields.html.twig and I don't know what I'm  doing wrong. My view is a block, I used the notation views-view-fields--cajas-de-enlaces.html.twig for my specific view's name, but when I flush the cache and reload the page The block is empty. Thanks

Dubs’s picture

For anyone stuck on this I would recommend adding a variable in a pre_process function in your .theme file and do the logic using PHP. You can get to the raw data much more easily.

Then TWIG can get to that new variable that you add to the $variables array.

Twig in intended to print markup primarily. Hidden fields are not passed in the fields array and the row object is very sparse too.

Please let me know if I've missed anything obvious, but this would be the approach I would recommend.