listing_save_pre
Filter the listing form data before saving it to the database.
Parameters#
$data
(array) form data
$params
(array) associative array with contextual data
You need to have a working knowledge of Hooks before you get started.
Boilerplate Code#
Use the boilerplate code to start using the filter, and add your own logic to modify the first argument and return it.
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
// Uncomment line below to view available data passed into hook
// For JReviews 3.x and earlier use dd instead of fwd_dd
// fwd_dd($data, $params);
return $data;
});
Examples #
- Automatically expire listings after one month
- Automatically set the listing expiration date using a custom field
- Automatically set the publication date using a custom field
- Calculate Years from Date
Automatically expire listings after one month#
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
// Don't do anything when listing is being updated
if (! $params['is_new']) {
return $data;
}
// Automatically set expiration date to 30 days from now
$data['Listing']['publish_down'] = \FWD\Carbon\Carbon::now()->addMonth();
});
Automatically set the listing expiration date using a custom field#
While it is possible to enable the expiration date field in listing types, if you have an event expiration custom field, you can avoid duplication of inputs by automatically setting the expiration date using this field.
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
$dateField = 'jr_eventend';
// Don't do anything if expiration date field is empty
if (empty($data['Field']['Listing'][$dateField])) {
return $data;
}
// Set listing expiration to the value of the jr_expirationdate custom field
$data['Listing']['publish_down'] = $data['Field']['Listing'][$dateField];
return $data;
});
Automatically set the publication date using a custom field#
While it is possible to enable the expiration date field in listing types, if you have an event start date custom field, you can avoid duplication of inputs by automatically setting the publication date using this field.
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
$dateField = 'jr_eventstart';
// Don't do anything if expiration date field is empty
if (empty($data['Field']['Listing'][$dateField])) {
return $data;
}
// Set publication date to 10 days before event starts
$data['Listing']['publish_up'] = \FWD\Carbon\Carbon::parse($data['Field']['Listing'][$dateField])->subDays(10);
return $data;
});
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
.
The calculation is only done when the listing is created and updated.
function calc_years_from_date($data, $params)
{
$dateField = 'jr_date';
$yearsField = 'jr_years';
if (empty($data['Field']['Listing'][$dateField]))
{
return $data;
}
$date = new DateTime($data['Field']['Listing'][$dateField]);
$today = new DateTime();
$diff = $today->diff($date);
$data['Field']['Listing'][$yearsField] = $diff->y;
return $data;
}
Clickfwd\Hook\Filter::add('listing_save_pre', 'calc_years_from_date', 10);
Source#
- /controllers/listings_controller.php