2

I would like to include a Users picker in a custom field type. Not sure if this is possible and practical.

Do you know how I can accomplish this, or can you point me in the right direction for self-education?

I see there is no users macro in /app/templates/_includes/forms.html, /app/fieldtypes/UsersFieldType.php has almost nothing in it, and I don't know enough about the system yet to know where else to look for ideas.

3
  • craft/app/templates/_includes/forms/elementSelect.html might be what your looking for, if that helps at all. Commented May 21, 2015 at 17:20
  • Very helpful, thanks @DouglasMcDonald! That lead me down a whole new rabbit hole, which quickly filled with water and other mixed metaphors. Commented May 22, 2015 at 15:41
  • haha... I know that feeling. Commented May 22, 2015 at 21:37

1 Answer 1

2

I don't know your requirements, but you could build a "Users Picker" with the Dynamic Fields plugin. You would basically need to do the same in its settings to build your select options as you'd do in a custom plugin.

You get your users with craft.users and then build an array with "label" and "value" keys using the merge filter.

Pass that array into the Dynamic Fields' settings or build the select options with the forms.selectField macro in your own plugin.

{% set users = craft.users.group('clients') %}

{% set userOptions = [] %}
{% for user in users %}
    {% set userOptions = userOptions|merge([{ label: user.friendlyName, value: user.id }]) %}
{% endfor %}

{{ forms.selectField({
    label: 'User'|t,
    id: id,
    name: name,
    options: userOptions,
    value: value ? value
}) }}
1
  • Thank you this is great, but I was hoping for the native Users picker that spawns a modal. There may be enough options that a select dropdown would be unwieldy. Due to time constraints I'll probably have to just use a separate Users field for now. Thanks again! Commented May 22, 2015 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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