Before Save
apply_filters_ref_array( 'vikappointments_before_save_option', bool $save, mixed &$data, JTable $table )
Fires before saving an option.
Description
This hook is triggered before creating or updating a service option record. It can be used to bind the data that are going to be saved within the database.
TIP: in case of failure while saving the record, it is possible to throw an exception to abort the saving process and return a readable error message. The same can be accomplished by registering an error to the table and returning false
.
// throw an exception
throw new Exception('Missing required field', 404);
// or register the error
$table->setError('Missing required field');
return false;
Parameters
- $save
-
(bool) Use false to abort the saving process.
- &$data
-
(array|object) Either an array or an object specifying the option properties to bind.
- $table
-
(JTable) The table instance that handles the saving process.
Example
The following example is used to save a new property that is not supported by default. It is assumed that the options database table has been altered to support the id_employee column.
/**
* Trigger hook to allow the plugins to bind the object that
* is going to be saved.
*
* @param boolean $save False to abort the saving process.
* @param mixed &$data The array/object to bind.
* @param JTable $table The table instance.
*/
add_filter('vikappointments_before_save_option', function($save, &$data, $table)
{
$input = JFactory::getApplication()->input;
// retrieve EMPLOYEE ID from request
$id_employee = $input->get('id_employee', null, 'uint');
// make sure the employee ID was set
if (!is_null($id_employee))
{
// bind array with employee ID to include it within the UPDATE/INSERT query
$data['id_employee'] = $id_employee;
}
return $save;
}, 10, 3);
Changelog
Version | Description |
---|---|
1.2.0 | Added $save and $table arguments. |
1.1.3 | Introduced. |