1

I am trying to update the value of the entry enabled field depending on a certain field value.

I am listening to the EVENT_BEFORE_SAVE event and have it updating as such $entry->enabled = $status == 'signedOff' ? true : false;

The issue I have is, if the entry is disabled at the start, then I edit the entry so that the status field to be signedOff, it updates the enabled value to true and it shows the lightbox field as being on, however it does not set a post date, and the entry can't be view on the frontend.

If i then resave the entry, it gets a post date and is visible on the frontend.

It seems that it may not be doing all it needs to when updating the enabled field to be true on the EVENT_BEFORE_SAVE event.

Is there something I need to call, or another way I need to achieve this for it to be done correctly?

I have other plugins which listen to the EVENT_AFTER_SAVE event, so ideally would like to do it using EVENT_BEFORE_SAVE, so that the entry does not have to be saved.

1 Answer 1

1

Looking into this more and understanding how Craft works, a post date needed to be set, it will not do this automatically for you.

On saving the entry, it will check to see if the entry is enabled and has a post date - https://github.com/craftcms/cms/blob/d72c2b0bbddfeced0920f11bfaf4d9891fe62a5a/src/elements/Entry.php#L936

To achieve this I copied how Craft manages the date in the beforeSave function https://github.com/craftcms/cms/blob/develop/src/elements/Entry.php#L1234-L1236

On testing this allowed the entry to be viewed in the frontend and allowed other plugins and modules to do what they needed to with the entry.

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.