0

I do not know if I am something missing, but I have a View in Drupal 8 and wanted to make a Template override in my theme, where I would print-out the image URL of one field and set it as "background-image" via inline CSS.

So I turned on theme debugging to see the template suggestions and picked views-view-field.html.twig, copied the file to my theme, renamed it to match the suggestion, cleared the cache and it looks like the template override is working.

But all the variables I am trying to print-out are blank, even after clearing the cache the field just disappeared. When I try to dump() or kint() any variable written in the file (view, field, row, output) it prints "null" or completelly nothing.

What am I doing wrong?

I am sure template override works, because when I type regular text in a file it gets printed out, but all the variables are somehow missing and I can not print anything out of them.

  • Does kint() print anything for you? It should at least print the vars you have access to in the scope of this template file. – cchen Feb 11 at 21:25
  • Are you trying to display some content type, paragraph? If you are using a display mode of a content type in your view, make sure this mode has the fields you want to display (i.e. the image) enabled – dimitriskr Feb 11 at 21:36
  • No, it only prints "null" and nothing else. It prints it out in Kint layout so I know Kint is working but nothing else for some reason. – Lub Feb 11 at 21:54
  • Well, I am using Views to display a list of articles and I want image URL to be as background-image property. So I need plain URL (easy to do via formatter) and I want it to be displayed in DIV as inline-style property. Thats where I need template overrides. So in theory view should have access to the field with no problems. – Lub Feb 11 at 21:55
  • Does the field happen to be excluded from Views display? – cchen Feb 11 at 22:25
0

Copy the content of the previous template used and start from there. It is most likely that the variable you are trying to display are not available in that template. Consider adding variables using hook_preprocess.

| improve this answer | |
  • Thats what I did. I copied file views-view-field.html.twig from core to my theme and flushed the cache. It does not ouput anything. I do not understand why it is like that. In Drupal 7 it worked nicely. Probably I came across some bug? – Lub Feb 12 at 13:45
  • I am sure you have already tried this but have you added the actual field name for the views-view-field.html.twig template? E.g. views-view-field--field-image.html.twig? – macjules Feb 12 at 15:14

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.