Triggers a filter event, allowing event subscribers to modify the specified data object. Each subscribed callback will be executed in order in a blocking/synchronous way.

Parameters:
NameTypeDescription
optsobject

The options object.

Properties
NameTypeDescription
dataobject

Data object sent to callbacks to be filtered.

eventstring

The event name.

Since
  • 3.0.1
Returns:

Returns the filtered data object.

Type: 
object
Example
import { filter } from "@gravityforms/utils";

async function Example() {
    let filteredData = 'hello';

    filteredData = await filter( { event: 'gform/example/event_name', data: filteredData } );
};

// elsewhere in the codebase

addFilter( 'gform/example/event_name', ( data ) => {
		data = 'hello again!';
		return data;
	}, 11 );

Requires

  • module:assign

Methods

(inner) addAsyncFilter(event, callable, priority) → {void}

Adds an async callback function to a filter event.

Parameters:
NameTypeDescription
eventstring

The event name.

callablefunction

The callback function to be executed. The callback function takes a 'data' parameter as argument that can be of any type (depends on the event being fired). Defaults to 10.

priorityint

The filter priority. This determines the execution order. Lower priority filters execute first.

Since
  • 3.0.1
Returns:
Type: 
void
Example
import { addAsyncFilter } from "@gravityforms/utils";

function Example() {

     addAsyncFilter( 'gform/example/event_name', async function ( data ) {
		    data = 'filtered data';
		    return data;
	    }, 10 );

};

// elsewhere in the codebase
let filteredData = 'hello';
filteredData = await filter( { event: 'gform/example/event_name', data: filteredData } );*

(inner) addFilter(event, callable, priority, isAsync) → {void}

Adds a callback function to a filter event.

Parameters:
NameTypeDescription
eventstring

The event name.

callablefunction

The callback function to be executed. The callback function takes a 'data' parameter as argument that can be of any type (depends on the event being fired). Defaults to 10.

priorityint

The filter priority. This determines the execution order. Lower priority filters execute first.

isAsyncboolean

Wether or not the callable parameter is an async function. Defaults to false.

Since
  • 3.0.1
Returns:
Type: 
void
Example
import { addFilter } from "@gravityforms/utils";

function Example() {

     addFilter( 'gform/example/event_name', function ( data ) {
		    data = 'filtered data';
		    return data;
	    } );

};

// elsewhere in the codebase
let filteredData = 'hello';
filteredData = await filter( { event: 'gform/example/event_name', data: filteredData } );*

(inner) removeFilter(event, priority, tag) → {void}

Removes a callback function from the list of filters.

Parameters:
NameTypeDescription
eventstring

The event name.

priorityint

The filter priority. Removes filters of this priority. If ommitted, filters of any priority will be removed.

tagstring

Removes functions of this tag. If ommitted, functions of any tag will be removed. Tag is formatted as 'event-name_function-index'.

Since
  • 3.0.1
Returns:
Type: 
void
Example
import { removeFilter } from "@gravityforms/utils";

function Example() {

  // removes all filters that are subscribed to 'gform/example/event_name' and that have priority 10.
  removeFilter( 'gform/example/event_name', 10);
};