2

I currently have checkboxes in a matrix block that allow a user to select where an image will be placed in the layout (as a featured slider image and/or in an image gallery). I've gotten the slider image to function but if, for some reason, there are no featured images I have a couple of unnecessary <div> tags. Because I am selecting only featured images from the matrix block I can not use loop.first and loop.last to move those divs in my for loop.

I had seen an article that seems close to finding the answer to my problem here : Only execute twig block if a Matrix radio has a certain value but I haven't been able to make it work.

Any help would be greatly appreciated.

{# Add Feature Images - Slideshow #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}
    {% if photos|length %}
    <div class="row" >
            <div class="slidr">
                {% for block in photos if block.imageLocation.contains('featured')%}
                    {# Get the photo #}
                    {% for photo in block.photo %}
                      <div class="figure">
                        <a href="{{ photo.getUrl }}">
                          <img src="{{ photo.getUrl('projectHero') }}" alt="">
                        </a>
                        {# Get the caption #}
                        {% if block.caption|length %}
                          <div class="caption">{{ block.caption }}</div>
                        {% endif %}
                      </div>
                    {% endfor %}
                {% endfor %}
            </div>
    </div>
    {% endif %}

I've attached a screen snapshot of my field setup. screen snapshot of matrix field

3
{% if block.checkboxFieldHandle.contains('someChkbxValue') %}
<p>Executed!</p>
{% endif %}

actual for Craft v2.x and v3.x

2

It looks as though you have the following line wrong:

{% for block in photos if block.imageLocation.contains('featured') %}

You will need to use the field name you're using to filter the Matrix block in place of contains. The following line will only loop through block with the imageLocation field set to "featured".

{% for block in photos.imageLocation('featured') %}

3
  • I had tried tried what you suggested but that gives all of the entries regardless if 'featured' is checked. Jan 3 '17 at 17:03
  • What is the error? Jan 3 '17 at 17:04
  • I see in the craft docs that "you can tell if the option is selected or not via option.selected." but I'm unsure how to make that work in the if statement. Like I mentioned in the original post. The above code works but I want to be able to hide the divs if there are no featured images. Jan 3 '17 at 17:10
1

I've managed to get things working but I'm not sure if there is a better way of doing this. Any feedback is welcome.

Here's the new code.

    {# Grab all the photo entries #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}

    {# create a blank array to merge content #}
    {% set selectedFeaturedImages = [] %}

    {% for block in photos %}
        {# Add Featured Images to an array #}
        {% if block.imageLocation.contains('featured') %}
            {% set selectedFeaturedImages = selectedFeaturedImages|merge([block]) %}
        {% endif %}
    {% endfor %}

    {% for block in selectedFeaturedImages %}
        {% if loop.first %}
        <div class="row" >
            <div class="columns">
                <div class="slidr">
        {% endif %}

            {% for photo in block.photo %}
        <div class="figure"><a href="{{ photo.getUrl }}"><img src="{{ photo.getUrl('projectHero') }}" alt=""></a>
            {# Get the caption #}
            {% if block.caption|length %}
            <div class="caption">{{ block.caption }}</div>
            {% endif %}
        </div>
            {% endfor %}

        {% if loop.last %}
                </div>
            </div>
        </div>
        {% endif %}
    {% endfor %}
1
  • I've marked this as the correct answer as I haven't had any feedback from anyone else and it is functioning properly. Jan 6 '17 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.