Answer the question
In order to leave comments, you need to log in
Is it possible to change the attributes of an object after processing in the underlying template?
Hello!
There is a site on Drupal 8, and downgraded Twig 1.3, you need to add a class and a couple of data-attributes to the image displayed in a certain block.
<a class="case-box__link-cover link-inline" href="#case_314">
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'field' -->
<!-- FILE NAME SUGGESTIONS:
* field--node--field-case-cover--case.html.twig
* field--node--field-case-cover.html.twig
* field--node--case.html.twig
* field--field-case-cover.html.twig
x field--image.html.twig
* field.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/salesvp/templates/field--image.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image_formatter' -->
<!-- BEGIN OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image_style' -->
<!-- BEGIN OUTPUT from 'core/modules/image/templates/image-style.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image' -->
<!-- BEGIN OUTPUT from 'themes/bootstrap/templates/system/image.html.twig' -->
<img src="/sites/default/files/styles/standartnyi_720x405_/public/covers/unihim_prew_img.jpg" class="img-responsive" width="720" height="405" alt="" typeof="foaf:Image">
<!-- END OUTPUT from 'themes/bootstrap/templates/system/image.html.twig' -->
<!-- END OUTPUT from 'core/modules/image/templates/image-style.html.twig' -->
<!-- END OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
<!-- END OUTPUT from 'themes/salesvp/templates/field--image.html.twig' -->
</a>
{% for item in items %}
{# Попытался добавить класс и свой атрибут #}
{{ item.attributes.addClass('responsively-lazy') }}
{{ item.attributes.setAttribute('srcset', 'data:image/gif;base64,R0lGODdhAQABAPAAACwsLAAAACwAAAAAAQABAAACAkQBADs=') }}
{# Ну и вывод #}
{{ item.content }}
{% endfor %}
<a class="case-box__link-cover link-inline" href="#case_314">
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'field' -->
<!-- FILE NAME SUGGESTIONS:
* field--node--field-case-cover--case.html.twig
* field--node--field-case-cover.html.twig
* field--node--case.html.twig
x field--field-case-cover.html.twig
* field--image.html.twig
* field.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/salesvp/templates/field--field-case-cover.html.twig' -->
class="responsively-lazy"
class="responsively-lazy" srcset="data:image/gif;base64,R0lGODdhAQABAPAAACwsLAAAACwAAAAAAQABAAACAkQBADs="
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image_formatter' -->
<!-- BEGIN OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image_style' -->
<!-- BEGIN OUTPUT from 'core/modules/image/templates/image-style.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image' -->
<!-- BEGIN OUTPUT from 'themes/bootstrap/templates/system/image.html.twig' -->
<img src="/sites/default/files/styles/standartnyi_720x405_/public/covers/unihim_prew_img.jpg" width="720" height="405" alt="" typeof="foaf:Image" class="img-responsive">
<!-- END OUTPUT from 'themes/bootstrap/templates/system/image.html.twig' -->
<!-- END OUTPUT from 'core/modules/image/templates/image-style.html.twig' -->
<!-- END OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
<!-- END OUTPUT from 'themes/salesvp/templates/field--field-case-cover.html.twig' -->
</a>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question