1

I currently have front-end code working for a user to create an entry, add a title (text input) and also relate entries via the entries field type and checkboxes. All on a front-end application.

Creating the entry is fine, but I'm having an issue with the editing of the entry.

I'm unable/unsure how to "check" what the previously selected relate entries where. Code for showing entry items...

{% set entries = craft.entries.section('exercises') %}
{% for entry in entries %}
<li>
    <input type="checkbox" name="fields[planItems][]" 
           id="fields[planItems]" value="{{ entry.id }}">{{ entry.title }}
</li>

Other fields are fine such as the title field, all you have to add is {%- if entry is defined %} value="{{ entry.title }}"{% endif -%}>.

Any ideas on how to get that value so I can add checked to the previously selected entries?

Thanks!

2

There is a in operator to check if a variable is in a string or an array. Your entries field actually contains an object of type ElementQuery instead of an array of elements.

All you need to do is to check if the current entry is in the list of selected entries

{% set selectedEntries = entry.planItems.ids() %}

{% set entries = craft.entries.section('exercises') %}
{% for entry in entries %}
    <li>
        <input 
            {% if entry.id in selectedEntries %}checked{%endif%}
            type="checkbox" 
            name="fields[planItems][]" 
            value="{{ entry.id }}">{{ entry.title }}
    </li>
{% endif %}
  • You legend Robin, thank you for that information. I never realized that in operator before and I totally had no idea how to check if the current entry is in the list. Quick typo on the last endif, just had to change that to endfor. Thanks so much again for your help! – Kieron Keenan Aug 25 '18 at 18:31

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.