JReviews logo
Guides My site
← All guides Community guide

Protect custom fields on listing updates

E
Thread started by Equity. Contributors Equity.
Thread started
Last updated

What you’ll achieve

Allow users to complete selected fields when creating a listing, then prevent non-admin users from changing those fields on updates.

My site

Platform
JReviews version

Allow users to fill selected custom fields when creating a listing, then prevent them from changing those fields on later edits. On a paid vehicle-classifieds site, this prevents a seller from replacing a sold vehicle with another vehicle in the same paid listing.

Choose the fields to protect

The examples protect vehicle fields jr_year, jr_make, jr_model, jr_trim, and jr_vin. Replace these names with your fields. Protect dependent fields together. For example, if Make controls the models available in Model, and Model controls the choices in Trim, include all three fields. Keep the fields assigned to the listing type, with their existing types and stored options.

JReviews 6 Early Access

Add the PHP code to your overrides

Enable Listing Form in Early Access.

Joomla. Add the PHP example to templates/jreviews_overrides/hooks.php.

WordPress. Add the PHP example to jreviews_overrides/hooks.php.

Start a new file with <?php followed by defined('MVC_FRAMEWORK') or die;. If the file exists, append the registration without repeating those lines. Keep the customization in overrides rather than editing JReviews core files.

The customization hides those fields on non-manager edit forms and restores their stored values immediately before saving. Hiding a control alone does not protect it from a manually constructed submission.

function guide_protected_listing_field_names(): array
{
    return ['jr_year', 'jr_make', 'jr_model', 'jr_trim', 'jr_vin'];
}

fwd_add_filter('jreviews:listing_form.custom_fields', function ($fields, $listing) {
    if (!$listing->exists || fwd_auth()->user()->isManager()) {
        return $fields;
    }

    return $fields->reject(fn ($field) => in_array($field->name, guide_protected_listing_field_names(), true));
}, 20, 2);

fwd_add_filter('jreviews:listing.data_before_save', function (array $data, $listing): array {
    if (!fwd_request()->inFrontend() || !$listing->exists || fwd_auth()->user()->isManager()) {
        return $data;
    }

    foreach (guide_protected_listing_field_names() as $name) {
        if ($listing->getField($name)) {
            $data['fields'][$name] = $listing->getFieldValue($name);
        }
    }

    return $data;
}, 20, 2);

Change the field list in guide_protected_listing_field_names() once. These hooks preserve existing permissions; they do not grant managers access to fields denied by the normal field settings. A new listing keeps its entered values. The save hook is limited to the frontend and does not change imports or control-panel saves.

JReviews 5 and v6 Legacy

Add the code to your overrides

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

Start a new file with <?php followed by defined('MVC_FRAMEWORK') or die;. Add the examples within that PHP file without repeating its opening tag or guard.

Protect selected fields after a listing is created

Use the can_write_field filter when users need to enter a field on a new listing but must not change it later. For vehicle classifieds, locking vehicle details prevents a seller from reusing the same paid listing to advertise a different vehicle.

Use your chosen field names in $fieldNames.

//
//Allow Admin only to edit year, make model, vin custom fields on listing edit page
//
Clickfwd\Hook\Filter::add('can_write_field', function($permission, $params)
{
    $auth = S2Object::make('auth');

    $fieldNames = ['jr_year','jr_make','jr_model','jr_trim', 'jr_vin'];

    if ($auth->admin || $params['referrer'] !== 'listing.update')
    {
        return $permission;
    }

    if (in_array($params['field']['name'], $fieldNames))
    {
        return false;
    }

    return $permission;
});

Administrators keep their normal field permissions. For other users editing an existing listing, the code denies changes to the named fields. New-listing forms and other fields keep their normal permissions. The code does not grant access denied by another rule.

Preserve dependent field values

Fields without dependencies can use the filter directly. Fields controlled by another field need extra care. If you restrict a parent field but leave a dependent child editable, the child can appear on the edit form without its saved value. Saving that form can remove the child's stored data.

For the example dependency chain, jr_make controls jr_model, and jr_model controls jr_trim. Include the dependent child fields in the restriction, as the full example does for jr_trim. Check the entire dependency chain before using this filter on existing listings.

Check creation and later edits

Create a listing as a regular user and confirm that protected values save. Reopen the edit form and check that the protected controls are absent. Change an unprotected field and save. Confirm that all protected values remain intact, including dependent fields. Submit an invalid unprotected required field and confirm that normal validation rejects it.

JReviews 6 Early Access. Submit a request containing a different protected value and confirm that the stored value remains unchanged. Edit as a manager and confirm that normal field permissions still apply.

JReviews 5 and v6 Legacy. Check every parent and dependent child field after saving. Repeat as an administrator and confirm that existing field permissions still apply.

Learn more