3

I need to set programmatically the value of an Entries field type when I'm going to create a new entry from the admin form, after the form loads I'm searching for the entry I need to set in the Entries field, so I'm making something like setAttribute('companyField', $companyEntry) but it didn't work.

I need to find a way to set/lookup that value in the Entries field type so the user can see it in the UI.

1

For relationship fields (Entries, Assets, Users, Tags, etc.) you need to pass in an array of element IDs.

Something like this should work.

$entry->setAttribute('companyField', array($companyEntry->id));
| improve this answer | |
  • cool, that works fine, i'll mark your answer as correct but could you tell me how can i do if i'm creating a new entry, i mean, i don't know how can i get the fields from that new entry i'm about to create. thanks – user2996243 Aug 31 '16 at 19:36
  • for entries that are currently created i can use the entryId to get the entry and update that entry field, but in this case i don't have an entryId – user2996243 Aug 31 '16 at 19:39
  • I don't know your front-end workflow, but either a user is selecting an already existing entry for the relationship to work, or your plugin is creating it at runtime before you save it, so your main $entry should have knowledge of it. – Brad Bell Sep 1 '16 at 3:36

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.