Jump to content

Filter - Prevent Changes to Custom Fields Content on Listing Updates


Equity

Recommended Posts

Hello,

Here is a sample of the filter hook for can_write_field found in the documentation sample code.

I wanted to use this on a vehicle classifieds site where users have access to all fields when submitting a new listing but restrict certain fields from edit so if the vehicle sells before the classified listing expires the user cannot edit to sell another vehicle in the original listing without purchasing a new listing.
 

 <?php
defined('MVC_FRAMEWORK') or die;
//
//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;
});

NOTE: Uncontrolled fields work with this solution just fine but will not work correctly if child fields rely on a control field included in the filter.

Any controlled child field not listed in the $fieldnames = [ ]; will still show on the edit form but the values are removed from the edit form because they rely on the parent and upon update or save of the edited listing will remove that child data from the listing all together.

In my case the controlled fields are set up as  jr_make (not controlled) >> jr_model (controlled by jr_make) >> jr_trim (controlled by jr_model).

If only including:

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

If only restricting jr_make and jr_model of the controlled fields and not jr_trim or any other controlled child field the issue will occur.

Cheers!

 

 

Edited by Equity
Link to comment
×
×
  • Create New...