Am poru Am poru - 22 days ago 7
Twig Question

Drupal 8 missing data when changing content

On drupal 8, I created a template as suggested by debug. My code looks like this:

{% for key, element in elements if key|first != '#' %}
{# element.content.field_shortcut_image #}
<a href="{{ element.content.field_shortcut_url.0['#context'].value }}" class="col-sm-12 col-md-5ths col-lg-5ths" >
<div class="shortcut-bg" style="background: transparent url({{ file_url(element.content.field_shortcut_image.0['#item'].entity.uri.value) }}) no-repeat center center;"></div>
<div class="shortcut-title">
{% if element.content.field_shortcut_icon.0['#item'].entity.uri.value %}
<img src="{{ file_url(element.content.field_shortcut_icon.0['#item'].entity.uri.value) }}" class="shortcut-icon" alt="{{ element['#configuration'].label }} icon" >
{% endif %}
{{ element['#configuration'].label }}
</div>
</a>
{% endfor %}


However, everytime i make any changes on drupal, like maybe update a content, that is not even related to that block, the data disappears. It looks like this when the data is present:
http://prntscr.com/d2wmku

and looks like this after changing or adding any content:
http://prntscr.com/d2wmxu

The data comes back though when i clear cache in admin/config/development/performance, but it is a pain, since each time you create/edit a content it disappears. Any idea what is happening here?

Answer

So for those noobs like me, maybe this will help.

Instead of getting the data from the #elements, i tried to use 'content' array, and that solved the issue for me. The data does not disappear on the content array when updating any content. my code looks like this:

{% set sicon = content.field_shortcut_icon.0['#item'].entity.uri.value %}
{% set sbg = content.field_shortcut_image.0['#item'].entity.uri.value %}
{% set slink = content.field_shortcut_url.0['#context'].value %}