The `onContentBeforeSave` and `onContentAfterSave` events in Joomla differ in when they are triggered and what they are typically used for:
onContentBeforeSave
- Triggered just before an item is saved to the database[1][2]
- Typically used to alter the data that is about to be saved or perform actions just before the data is saved[1]
- Can be used to determine whether an item can be saved by returning a boolean value (true to allow saving, false to abort)[2]
- Should be used with a condition to check the context (e.g., `com_content.form` for articles) to avoid running for all content types[1]
onContentAfterSave
- Triggered just after an item is saved to the database[1][4]
- Typically used to perform actions after the data is saved, such as redirecting to a specific page or performing additional database activities[1]
- The `com_content` frontend form model extends the backend model, which in turn extends `Joomla\CMS\MVC\Model\AdminModel`, allowing `onContentAfterSave` to work in the front end[4]
In summary, `onContentBeforeSave` is used for modifying or validating data before it is saved, while `onContentAfterSave` is used for performing post-save actions. Both events should be used with context checks to ensure they run only for the desired content type.
Citations:[1] https://www.itoctopus.com/on-the-joomla-oncontentbeforesave-and-oncontentaftersave-events
[2] https://joomla.stackexchange.com/questions/32326/plugin-to-manipulate-form-data-oncontentbeforesave
[3] https://stackoverflow.com/questions/24534181/oncontentbeforesave-and-oncontentaftersave-not-triggering
[4] https://joomla.stackexchange.com/questions/25145/how-is-oncontentaftersave-working-in-the-front-end
[5] https://gist.github.com/ryandemmer/d34bd48bd7539e87f0d262f51f6ad0fc