jreviews:listing.data_before_save
Allows filtering and modifying listing data array before it's saved to database. Useful for automated data processing, calculations, and field dependencies.
You need to have a working knowledge of Hooks before you get started.
Fires in listing save action, before data is written to database, after validation
Parameters
| Name | Type | Description |
|---|---|---|
$data |
array |
The listing data array with 'fields' key for custom fields and top-level keys for standard fields |
$listing |
\JReviews\App\Models\Listing |
The listing model being created or updated |
Boilerplate Code
Use the boilerplate code to start using the filter, and add your own logic to modify the first argument and return it.
fwd_add_filter('jreviews:listing.data_before_save', function($data, $listing)
{
// Your code here
return $data;
});
Examples
- Automatically Expire Listings After One Month
- Automatically Set Listing Expiration Date Using Custom Field
- Automatically Set Publication Date Using Custom Field
- Calculate Years From Date
Automatically Expire Listings After One Month
Set expiration date to 30 days from now when creating new listings.
fwd_add_filter('jreviews:listing.data_before_save', function($data, $listing)
{
// Don't do anything when listing is being updated
if ($listing->exists) {
return $data;
}
// Automatically set expiration date to 30 days from now
$data['expiration_date'] = \FWD\Carbon\Carbon::now()->addMonth()->format('Y-m-d H:i:s');
return $data;
}, 10, 2);
Automatically Set Listing Expiration Date Using Custom Field
If you have an event end date custom field, automatically set the listing expiration date to match it, avoiding duplicate inputs.
fwd_add_filter('jreviews:listing.data_before_save', function($data, $listing)
{
$dateField = 'jr_eventend';
// Don't do anything if expiration date field is empty
if (empty($data['fields'][$dateField])) {
return $data;
}
// Set listing expiration to the value of the jr_eventend custom field
$data['expiration_date'] = $data['fields'][$dateField];
return $data;
}, 10, 2);
Automatically Set Publication Date Using Custom Field
If you have an event start date custom field, automatically set the publication date to 10 days before the event starts.
fwd_add_filter('jreviews:listing.data_before_save', function($data, $listing)
{
$dateField = 'jr_eventstart';
// Don't do anything if date field is empty
if (empty($data['fields'][$dateField])) {
return $data;
}
// Set publication date to 10 days before event starts
$data['publication_date'] = \FWD\Carbon\Carbon::parse($data['fields'][$dateField])
->subDays(10)
->format('Y-m-d H:i:s');
return $data;
}, 10, 2);
Calculate Years From Date
Takes the value from a date custom field 'jr_date', calculates the number of years from that date until today, and stores the result in another field 'jr_years'.
fwd_add_filter('jreviews:listing.data_before_save', function($data, $listing)
{
$dateField = 'jr_date';
$yearsField = 'jr_years';
if (empty($data['fields'][$dateField])) {
return $data;
}
$date = new DateTime($data['fields'][$dateField]);
$today = new DateTime();
$diff = $today->diff($date);
$data['fields'][$yearsField] = $diff->y;
return $data;
}, 10, 2);
Source Files
app/Actions/StoreListingAction.php