4

With my plugin I listen for onBeforeSaveEntry events and make changes to the $entry EntryModel. It works and I can change most properties from this function, but I have problems updating sectionId, typeId, authorId, postDate and expiryDate.

I looked up the relevant code in EntriesService.php and noticed that it might have something to do with a variable $entryRecord that gets saved to the DB separately (comment in the code: "Save the actual entry row").

Am I doing something wrong or is it simply not possible to set these properties from this function. As an alternative solution I could trigger a seperate saveEntry() from here, but this looks to be not the cleanest solution.

craft()->on('entries.onBeforeSaveEntry', function(Event $event)
{
    // Disable the entry
    $event->params['entry']->enabled = false;

    // Change the `authorId` (does NOT work!)
    $event->params['entry']->authorId = 1;
});

1 Answer 1

2

Just looking through the source file, it looks like sectionId, typeId, authorId, postDate and expiryDate are all set as properties on the $entryRecord before the onBeforeSaveUser event is fired. It also looks like $entryRecord is not accessible from the event, nor is it saved yet such that you could modify it directly. I suspect that you will need to modify these properties after the entry is saved (using the onSaveEntry event), and then resave the entry. You will also of course need to add conditionals (i.e. compare current authorId to target authorId) and only save the entry if needed, to ensure that you don't create an endless loop.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.