1

I have a channel which has a relate to user field, how do I setup the front-end form which is a user details update form so they can select a channel entry from a drop down and when saved, have it relate that user to the channel entry.

4

When saving an entry field type as part of your plug-in it needs to be an array of IDs in order to work correctly. This can either be handled via a plug-in or in the form by passing the value as an array to that field handle.

Plug-in:

$user->setContentFromPost(array(
    'entryField' => array(10)
));
$success = craft()->users->saveUser($user);

Front-end form:

<input type="hidden" name="fields[relatedEntry][]" value="{{entry.id}}">
| improve this answer | |
  • Ah my bad, i dont think i have explained it well. The channel entries already exist, when a user updates their profile from a front end form i have created, they get to choose a channel entry from a drop down list. Problem i am having is that by selecting a channel entry and saving, the related field in their profile is remaining empty. Basically, what do i do to the form field in the user update form, for craft to relate the user to the chosen channel entry. – BillyMedia Apr 7 '16 at 13:02
  • So you want them to be able to choose a specific entry to be related to their profile? – mcclaskiem Apr 7 '16 at 13:04
  • That is correct. – BillyMedia Apr 7 '16 at 13:04
  • My dropdown list of channel entries consists of the entry.id for the value, and entry.title for the label. and the field has a name of fields[myrelatedfield]. – BillyMedia Apr 7 '16 at 13:06
  • updated answer, working on a code example now! – mcclaskiem Apr 7 '16 at 13:08

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.