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

<p>On saving the entry, it will check to see if the entry is enabled and has a post date - <a href="https://github.com/craftcms/cms/blob/d72c2b0bbddfeced0920f11bfaf4d9891fe62a5a/src/elements/Entry.php#L936" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/d72c2b0bbddfeced0920f11bfaf4d9891fe62a5a/src/elements/Entry.php#L936</a></p>

<p>To achieve this I copied how Craft manages the date in the <code>beforeSave</code> function <a href="https://github.com/craftcms/cms/blob/develop/src/elements/Entry.php#L1234-L1236" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/develop/src/elements/Entry.php#L1234-L1236</a></p>

<p>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.</p>

CC BY-SA 4.0
2020-04-17T10:16:26.337Z
2020-04-17T10:16:26.337Z
zizther
<p>I am trying to update the value of the entry enabled field depending on a certain field value.</p>

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

<p>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 <code>signedOff</code>, it updates the enabled value to <code>true</code> 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.</p>

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

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

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

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

<events><modules><saveentry><status>
Update entry Enabled value based on field value when saving
# Update entry Enabled value based on field value when saving

(Asked by: username_0 on 2020-04-16)

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.

**Tags:** events, modules, saveentry, status

---

## 1 answers

### Answer by username_0 on 2020-04-17. Score: 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.


---

