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 %}
                        {{ block.referenceText }}
                        <span class="text-sm italic">{{ block.referenceSource }}</span>
                    {{ block.referenceLink|length ? "</a>" }}
                    {% endif %}
                {% endif %}
            {% endfor %}
{% 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.

  • Could you share some of your code?
    – Oli
    Commented Jan 7, 2021 at 7:26
  • I added some code
    – Matthias
    Commented Jan 7, 2021 at 7:47
  • 1
    I'm not seeing the thing as you. For me, your code works and hides. What Propagation Method do you have on your Matrix field? Also, is entry the default auto-injected entry variable on an entry page?
    – Oli
    Commented Jan 7, 2021 at 8:16
  • I have a feeling you are not using Only save blocks to the site they were created in as your Propagation Method.
    – Oli
    Commented Jan 7, 2021 at 8:59
  • Thanks for reminding me of the propagation methods. I have fields with different methods. I prefer consistency for all matrix fields for our users. I don't want to use Only save blocks to the site they were created in in most of the cases, but I may use them for some matrix fields. However, also deleting matrix entries behave differently; with this option they only get deleted in the current language, but with the other option, they get deleted in all the other sites as well. A bit confusing for our users, but I can add a description to the fields that behave differently.
    – Matthias
    Commented Jan 9, 2021 at 7:04


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.