3

I have a user field called "designOptions" that is a Categories Checkbox field type. I have this field in the Edit Profile front-end form so that the user can see the saved options that they had selected previously (if any) and then change or add to those selections in the Edit Profile form. I am having a hard time finding anything in the documentation or SE about how to display all of the user's category checkbox options while displaying the options that are already selected in their account for that field. This is what I have so far, but it is checking all category options instead of just the 4 that the user has selected - likely because the length conditional is only looking at whether this field has 'any' value and not checking the value of each option.

I would appreciate any guidance you can give on the best way to do this. :)

Thank you!

{# Grab all categories in the group and set variable. #}
{% set categories = craft.categories.group('designCategories') %}

{# Grab the categories that the user already selected #}
{% set userOptions = user.designOptions %}

{# Display all category checkboxes in the form, with the logged in user's saved selections already checked.#}
{% for category in categories %}
    <input type="checkbox" id="designOptions" name="fields[designsOptions][]" {% if userOptions | length %}checked{% endif %} value=" {{ category.id }}"> {{ category.title }}</input>
{% endfor %}

Design CategoriesDesign Options User Field with Design Categories as SourceCategories already selected by UserTrying to display all categories as well as the categories already selected by the user on the "Edit Profile" front end page.

  • I'm a little confused... "designOptions" is a Checkboxes field type that's on your User field layout, correct? What's the relationship between "designOptions" and your category group, "designCategories"? – Brad Bell Sep 5 '14 at 19:46
  • I created a User field 'designOptions' that is a "Category Field Type" which calls on options from the category group (designCategories). On the Edit Profile page, I would like to display all of those categories as checkboxes with the options already saved showing as checked. This will allow the user to either uncheck current categories or check new categories in the "Edit Profile" form. – Angela Sep 5 '14 at 20:54
  • I'm not sure that helps my brain. :) Can you post some screenshots in the original question of all of the involved fields and what field layouts they are attached to? – Brad Bell Sep 5 '14 at 22:22
  • Here you go @BradBell Hopefully this will help. :) – Angela Sep 5 '14 at 23:05
0

Try something like this (completely untested):

{# Grab all categories in the group and set variable. #}
{% set categories = craft.categories.group('designCategories') %}

{# Grab the categories Ids that the user already selected #}
{% set userOptionIds = user.designOptions.limit(null).ids() %}

{# Display all category checkboxes in the form, with the logged in user's saved selections already checked.#}
{% for category in categories %}
    <input type="checkbox" id="designOptions" name="fields[designsOptions][]" {% if category.id in userOptionIds %}checked{% endif %} value=" {{ category.id }}">
        {{ category.title }}
    </input>
{% endfor %}
1

I realize this thread has an accepted answer, but I wanted to provide another solution I was able to come up with because of the answer Brad provided. In my example, I am working with a plugin, but the twig/html is the same plugin or not.

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.