JReviews logo Docs
Menu
Version

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.

Filter
Listings
Since 6.0.0

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;
}, 20, 2);

The , 20, N after your callback are the hook priority and the number of arguments your callback accepts. By default, a hook passes your callback only its first argument; for a filter, that is the value being filtered, so a simple function($value) { ... } needs nothing extra. If your callback declares more parameters, such as function($value, $listing) { ... }, you must add N (the parameter count, 2 here). Because N is the fourth argument to fwd_add_filter() or fwd_add_action(), you must also pass the priority (20 is the default). Leaving these off when your callback expects extra parameters causes a Too few arguments to function ... fatal error.

Development & Support
Customizations are not included with support. We provide this information to make it easier for developers to extend the functionality. From time to time we may have some availability for custom work. Get in touch to see if there's an opportunity to work together.

Examples

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;
}, 20, 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;
}, 20, 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;
}, 20, 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;
}, 20, 2);

Source Files

  • app/Actions/StoreListingAction.php