I'm trying to update an entry's field value from a Craft plugin. First I was trying to just update the attributes of the entry like so:
$entry->getContent()->setAttributes(array(
'metaStatus' => 'Declined'
));
craft()->entries->saveEntry($entry);
But that wouldn't work because getContent
doesn't return the data needed in order for the EntryModel
to pass validation. The next solution was to loop through each of the fields and grab their field value like so:
$fields = array();
foreach($entry->getFieldLayout()->getFields() as $layout) {
$handle = $layout->getField()->handle;
$fields[$handle] = $entry->getFieldValue($handle);
}
$fields['metaStatus'] = 'Declined';
$entry->setContent($fields);
craft()->entries->saveEntry($entry); // FAIL
Now I'm having issues with matrix/asset fields validating since they come back as empty. Am I going to have to look for matrix/asset fields in my loop above and correctly format the data? There must be an easier way to do this.