0

I have a View of Article nodes here which uses this Views field template, copied from core, to target the Article's image field:

views-view-field--myarticles--page_1--field-image.html.twig

I can see on the resulting Views page that my template file is working ok and includes {{ dump() }} but how do I rewrite the default core template to say for example "print the url (ie file path) for this image"?

Original core template ( /web/core/modules/views/templates/views-view-field.html.twig ) =

{#
/**
 * @file
 * Default theme implementation for a single field in a view.
 *
 * 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]
 *
 * The above will guarantee that you'll always get the correct data, regardless
 * of any changes in the aliasing that might happen if the view is modified.
 *
 * @see template_preprocess_views_view_field()
 *
 * @ingroup themeable
 */
#}
{{ output -}}
4
  • Wouldn't it be easier to just change the formatter for that field to URL to image in the View?
    – Clive
    Commented Jun 7 at 15:03
  • @Clive I have kept my question simplified but the reason I need to use a twig template is that I need to use an SVG tag and the image file path would be part of that SVG - but Views rewrite field output for some reason does not allow the SVG tag as reported here drupal.org/project/drupal/issues/2840938 but still not fixed after seven years.
    – Juc1
    Commented Jun 7 at 17:32
  • Yeah I was suggesting you use both - if you change it to Url to image in the View, then in the template it's just <img src="{{ row[field.field_alias] }}" />. If you need to know the template name to override, enable twig debugging and you'll see some suggestions in the markup
    – Clive
    Commented Jun 7 at 19:17
  • but still not fixed after seven years. @Juc1, you can always use and apply the patch that's there using Composer.
    – No Sssweat
    Commented Jun 8 at 22:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.