1

I have created an event that is called on beforeSave within my plugin. Code below:

Event::on(
        Entry::class,
        Entry::EVENT_BEFORE_SAVE,
        function(ModelEvent $event){
            /** @var Entry $entry */
            $entry = $event->sender;

            if($entry->section == 'Question' && $entry->type=='reply') {
                if($entry->newParentId != $entry->question->id[0]){
                    $entry->newParentId = $entry->question->id[0];
                }
            }
        });

I can change things like title and that works fine but the parent will never set. I have even added the following to see if this helps but it doesn't

if ($entry->newParentId) {
    $parentEntry = Craft::$app->getEntries()->getEntryById($entry->newParentId, $entry->siteId);
    if (!$parentEntry) {
         throw new Exception('Invalid entry ID: ' . $entry->newParentId);
    }
} else {
    $parentEntry = null;
}

$entry->setParent($parentEntry);

Please Note: This is not a custom element!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.