I have a channel
called "Groups", with a users
field type called members
for the entries. I need to be able to add/remove users to the members
entry field via plugin.
What I've tried is:
// criteria
$criteria = craft()->elements->getCriteria(ElementType::Entry);
// filter
$criteria->slug = 'groupNameHere';
// section
$criteria->section('groups');
// get entry
$entries = $criteria->find();
foreach($enries as $entry)
{
// get current members of group
$members = $entry['members'];
// add new user model to $members
$members[] = craft()->users->getUserByUsernameOrEmail("newUserEmailHere");
// update field with new $members array
$entry->getContent()->setAttribute('members', $members);
// save
craft()->entries->saveEntry($entry);
}
This simply clears the previous members
value for the group, without even adding the new user. I can't find anything online about updating this type of field-type via plugin so I'm just guessing how to do this. Does anyone know the right way to do this?
Any information would hugely help! Thanks!