1

In a component, if I want to add an user field to the form, I can easily use a field of type user, and play with it as I want.

But what can I do if I need a field that stores an array of users?

The idea is, in the backend, to be able to add one or more objects of type user to that field (let's call it, for example, collaborators), and in the frontend to be able to manipulate and show the list of users selected for that field (in the collaborators example, it could be something like Collaborators: Sophia Smith, Emma Williams, Oliver Lee.).

Edit: same question extended to an array of objects of type contact, url, etc...

  • 1
    Perhaps use subform fields and each subform field can contain a user field? – Lodder Jul 25 '18 at 16:35
  • @Lodder I'm giving a try to the subform option. But it's the first time I use multiple attribute, and I'm having hard time finding out the way to store the value in the database. Any advice on that? – dolma33 Jul 26 '18 at 16:22
  • 1
    I've never used subforms before. I use the old repeatable form fields still as it's the only one I like. So sorry, I cant help you on that – Lodder Jul 26 '18 at 16:27
  • @Lodder Came out pretty straightforward using multiple attribute, once I've accepted to save string and not a real relationship. =D Do you have any suggestion on what to do in order to use objects of type contact of instead of using objects of type user? – dolma33 Jul 26 '18 at 19:34
2

User type field doesn't support multiple attribute. I can think of 3 options:

  1. Create a custom field type with multiple support. You can use Author field as example. Just change the query to be a simple select from #__users table https://github.com/joomla/joomla-cms/blob/e6d61160eff61d749328e86c01c98fc5239a198a/libraries/src/Form/Field/AuthorField.php.

  2. Use SQL field. See documentation here https://docs.joomla.org/SQL_form_field_type.

  3. Use Subform field type and include User type field in it. https://docs.joomla.org/Subform_form_field_type

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.