0

Our content editors have the option of placing a button block in layout builder with a series of buttons on two rows if checkbox is checked. If checked, the value is "On". This worked in Drupal 9 with the "if empty" coupled with the "for" loop, but new requirements in D10 are to separate this out. When we do this it negates the two row request and puts all the buttons on the second row. How can we reimplement the 2nd row option with the new for/if separated as required? Here's our code:

 {# if two rows is "on", counts number of button fields w/ url content uses number to loop through and separate into rows #}
        {% if "On" in content.field_two_rows.0 %}
            {% set btn_count = 0 %}
            {% for button in content.field_button_link %}
                {% if button['#url'] is not empty %}
                    {% set btn_count = btn_count + 1 %}
                {% endif %}
            {% endfor %}

            <div class="row two-btn-row btn-row-one">
            {% for button in content.field_button_link %}
                {% if button['#url'] is not empty %}
                    {% if loop.index <= (btn_count / 2) %}
                        <div class="col-md mt-4">
                            <a{{btn_attribute.addClass(btn_classes)}} href="{{ button['#url'] }}">
                                {{ button['#title']|striptags('<i>')|raw }}
                            </a>
                        </div>
                    {% endif %}
                {% endif %}
            {% endfor %}
            </div>
            <div class="row two-btn-row btn-row-two">
                {% for button in content.field_button_link %}
                    {% if button['#url'] is not empty %}
                        {% if loop.index > (btn_count / 2) %}
                            <div class="col-md mt-4">
                                <a{{btn_attribute.addClass(btn_classes)}} href="{{ button['#url'] }}">
                                    {{ button['#title']|striptags('<i>')|raw }}
                                </a>
                            </div>
                        {% endif %}
                    {% endif %}
            {% endfor %}
            </div>
        {% else %}
            <div class="row one-button-row">
                {% for button in content.field_button_link %}
                    {% if button['#url'] is not empty %}
                    <div class="col-md mt-4">
                        <a{{btn_attribute.addClass(btn_classes)}} href="{{ button['#url'] }}">
                            {{ button['#title']|striptags('<i>')|raw }}
                        </a>
                    </div>
                    {% endif %}
            {% endfor %}
            </div>
        {% endif %}
5
  • Which new requirements in D10 are you referring to?
    – mona lisa
    Commented Oct 30, 2023 at 16:58
  • See drupal.stackexchange.com/questions/316574/…
    – 4uk4
    Commented Oct 30, 2023 at 17:24
  • @cilefen, It's really a Twig 3.x update and is about the move of an if statement inside a for tag to it's separate line drupal.org/node/3256890 Commented Oct 30, 2023 at 20:50
  • @4uk4 I did read that article and wasn't sure how to apply that to my situation. Commented Oct 30, 2023 at 20:50
  • This is a question on Twig 3 syntax?
    – mona lisa
    Commented Oct 30, 2023 at 21:13

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.