I'm trying to update the expiry field—based on an existing field (in this case classDate)—as soon as an entry is saved.
Based on this answer, the clever recursion code in carlcs' Auto Expire plugin, I was able to get it working with this code:
public function init()
{
craft()->on('entries.saveEntry', function(Event $event) {
$entry = $event->params['entry'];
if ($entry->section->name == "Classes")
{
$entry->expiryDate = $entry->classDate;
static $recursionLevel = 0;
if ($recursionLevel == 0)
{
$recursionLevel++;
$saved = craft()->entries->saveEntry($entry);
}
}
});
}
But that seems a little dirty because saveEntry is going to get called again.
Is there a better way to handle the recursion? Bob's answer here leads me to believe you can call craft()->elements->saveElement which won't raise the saveEntry event again but I couldn't get that to work.
What am I missing? Seems too easy...
array('expiryDate' => DateTimeHelper::formatTimeForDb($entry->classDate))not sure if there's a better way? – RitterKnight Jul 04 '15 at 04:51