0

I'm overriding the default views-view-field.html.twig for a specific field in a view.

The template is named:

views-view-field--conference_overview--page_1--field_media_oembed_video.html.twig

Note: this field is using a custom Field Formatter (the render array output of which is coming through fine.)

What works:

  • The template is loaded for the correct field, only that field and only that view, all as intended.
  • {{ output }} can be displayed correctly.
  • I can embed style attributes in the template file without them being stripped out by the XSS Filter (a problem I've had with direct Field Formatter output using templates and render arrays - I'm trying to work around that here by using a views template to do the 'style' bit, but some of the content of that still requires my Field Formatter)

What doesn't:

The docblock refers to other available variables such as view,field and row

{{ view }} produces:

Exception: Object of type Drupal\views\ViewExecutable cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter()

Fair enough, Twig can't print that, but if I use {{ kint(view) }} it doesn't seem to give me any output at all (page stops loading without errors etc.)

{{ kint(row) }} - runs out of RAM
{{ kint(row[field.title]) }} = NULL
{{ field.view_node }} nothing
{{ field.view_node.content }} nothing
{{ field.field_title }} nothing
{{ field.field_title.content }} nothing

... and other combinations (I've seen reference to 'fields' in other questions, and using bracket rather than dot syntax).

(I moved my field to after the others in the Views UI).

Any suggestions?

6
  • are you suer the template name views-view-field--conference_overview--page_1--field_media_oembed_video.html.twig works, the template name should be - not _ ? – berramou Dec 29 '19 at 17:29
  • @berramou hi - yep that part definitely works; however you're right of course and I've also just renamed it without the underscores to be sure. I've also simplified the view (got rid of the field formatter, scrapped most of the fields, removed relationships, tried field templates on other plain text fields etc.) - I'm not getting variables other than {{ output }} for any of them. I guess I should test on a clean core install next… – William Turrell Dec 29 '19 at 19:57
  • 1
    Yes i guess, and try with drupal.org/project/twig_vardumper to dump variables {{ dump(field) }} – berramou Dec 29 '19 at 20:33
  • 1
    You have to apply a method of the field plugin field on the variable row, see drupal.stackexchange.com/a/225347/47547 – 4k4 Dec 29 '19 at 21:27
  • Great tip with the new(ish) Twig module… I found it in {{ field.original_value }} – William Turrell Dec 29 '19 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.