I have an entry with matrix fields. If I add blocks in the main language, but its fields only contain data in one language, the block query (the for loop) still finds these empty fields since they do contain data in the other language. It is easy not to display the fields and their enclosing divs with an if statement.
However, if all the fields are empty, I don't even want to display the enclosing/parent div/title of all the blocks; and also do not want it to show on the page menu. I could loop through all of the blocks each time to find out if all the fields are empty — and then not display the enclosing title – but is there an easier way?
{% set referenceBlocks = entry.references.all() %}
{% if referenceBlocks|length %}
<div class="parent">
<div class="showMobile"><h3 class="title">{{ "References"|t }}</h3></div>
{% for block in referenceBlocks %}
{% if block.referenceLink|length or block.referenceText|length or block.referenceSource|length %}
<div class="w-full">
{% if block.referenceLink|length %}
<a target="_blank" href="{{ block.referenceLink|default }}">
{% endif %}
<p>
{{ block.referenceText }}
<span class="text-sm italic">{{ block.referenceSource }}</span>
</p>
{{ block.referenceLink|length ? "</a>" }}
{% endif %}
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
So I don't want to show the elements outside the for loop if all the blocks and fields in those blocks are empty. The first if statement only works when there are no matrix block in ALL languages, but when there are some in the main language but not in the other one, it will display the parent div and title.
entry
the default auto-injectedentry
variable on an entry page?