The `onContentBeforeSave` event in Joomla is used to determine whether an item can be saved. It is called right before the content is saved into the database. The plugin should return a boolean value, where `true` indicates that the item can be saved and `false` aborts the saving process.
Here is an example of how to use the `onContentBeforeSave` event:
php
function onContentBeforeSave($context, $article, $isNew) {
// Perform any necessary checks or modifications to the $article data
// Return true to allow the item to be saved, or false to abort the save
return true;
}
In this example, the `$context` parameter specifies the context of the content being saved, the `$article` parameter is the data being saved, and the `$isNew` parameter is a boolean indicating whether the content is being created or updated.
Parameters
- $context: The context of the content being saved, which includes the component name and view or the name of the module (e.g., `com_content.article`).
- $article: A reference to the `JTableContent` object that holds the article data.
- $isNew: A boolean indicating whether the content is being created (`true`) or updated (`false`).
Return Value
The `onContentBeforeSave` event should return a boolean value. If `true`, the item will be saved; if `false`, the saving process will be aborted.
Example Usage
Here is a more detailed example of how to use the `onContentBeforeSave` event:
php
function onContentBeforeSave($context, $article, $isNew) {
// Check if the article is being created
if ($isNew) {
// Perform any necessary actions for new articles
// For example, set a default category
$article->catid = 1;
}
// Return true to allow the item to be saved
return true;
}
This example demonstrates how to modify the article data before it is saved, specifically setting a default category for new articles.
Citations:[1] https://docs.joomla.org/Plugin/Events/Content
[2] https://stackoverflow.com/questions/24534181/oncontentbeforesave-and-oncontentaftersave-not-triggering
[3] https://joomla.stackexchange.com/questions/32326/plugin-to-manipulate-form-data-oncontentbeforesave
[4] https://gist.github.com/ryandemmer/d34bd48bd7539e87f0d262f51f6ad0fc
[5] https://github.com/joomla/joomla-cms-examples/blob/master/plugins/content/example/example.php