Filter Dynamic uncanny-automator

automator_option_{dynamic}_field

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters options for dynamic fields in automator configurations, allowing modification of field arguments and options before they are displayed or processed.

add_filter( 'automator_option_{dynamic}_field', $callback, 10, 2 );

Description

This dynamic filter hook allows developers to modify the arguments used to generate field configurations for specific automation options. It fires before the field arguments are returned, enabling customization of token names, token support, number constraints, and default token exclusion for individual option codes.


Usage

add_filter( 'automator_option_{dynamic}_field', 'your_function_name', 10, 2 );

Parameters

$option (mixed)
This parameter contains the value of the option being filtered, which can be of any mixed type.
$args (mixed)
The `$option` parameter contains the current value of the dynamic option being filtered.

Return Value

The filtered value.


Examples

/**
 * Example of modifying the 'automator_option_{dynamic}_field' filter.
 *
 * This filter allows developers to modify the arguments passed to a specific
 * dynamic field within the Automator plugin. In this example, we'll assume
 * there's a field with the option code 'custom_user_input_field' and we want
 * to set a default value and explicitly state that it supports tokens.
 *
 * The filter name is dynamically generated based on the $option_code passed
 * to the apply_filters() function in the source code.
 *
 * @param mixed $option The current value of the field option being filtered.
 * @param mixed $args   The arguments passed to the field.
 *
 * @return mixed The modified field option.
 */
add_filter(
	'automator_option_custom_user_input_field_field',
	function ( $option, $args ) {
		// Let's assume the default $option might be empty or null.
		// We'll set a default value if it's not already set.
		if ( empty( $option ) ) {
			$option = 'This is a default value.';
		}

		// Let's ensure that token support is explicitly enabled for this field.
		// The $args parameter likely contains an array of settings for the field.
		if ( isset( $args['supports_tokens'] ) ) {
			$args['supports_tokens'] = true;
		}

		// The filter expects the modified option to be returned.
		// In this case, we are primarily modifying $args, but the filter's
		// signature implies we might also modify $option if needed.
		// For this specific example, we return the potentially modified $option
		// as the primary return value, but the logic applied to $args might
		// indirectly affect how the field behaves later.
		// If the function applying this filter used $args directly without
		// re-assigning $option, we'd need to return $args instead or ensure
		// $option is updated based on $args. Given the context, returning $option
		// is standard for filter hooks that modify a primary value.
		return $option;
	},
	10, // Priority
	2  // Accepted arguments count
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/helpers/class-automator-recipe-helpers-field.php:67

public function create_field( $args = array() ) {
		$defaults              = array(
			'option_code'           => 'INT',
			'label'                 => esc_attr__( 'Number', 'uncanny-automator' ),
			'description'           => '',
			'placeholder'           => esc_attr__( 'Example: 1', 'uncanny-automator' ),
			'required'              => true,
			'input_type'            => 'int',
			'default'               => '',
			'token_name'            => '',
			'min_number'            => null,
			'max_number'            => null,
			'supports_tokens'       => true,
			'exclude_default_token' => false,
		);
		$args                  = wp_parse_args( $args, $defaults );
		$option_code           = $args['option_code'];
		$label                 = $args['label'];
		$description           = $args['description'];
		$placeholder           = $args['placeholder'];
		$required              = $args['required'];
		$default               = $args['default'];
		$token_name            = $args['token_name'];
		$supports_tokens       = $args['supports_tokens'];
		$input_type            = $args['input_type'];
		$min_number            = $args['min_number'];
		$max_number            = $args['max_number'];
		$exclude_default_token = $args['exclude_default_token'];

		$option = array(
			'option_code'           => $option_code,
			'label'                 => $label,
			'description'           => $description,
			'placeholder'           => $placeholder,
			'input_type'            => $input_type,
			'required'              => $required,
			'default_value'         => $default,
			'token_name'            => $token_name,
			'supports_tokens'       => $supports_tokens,
			'min_number'            => $min_number,
			'max_number'            => $max_number,
			'exclude_default_token' => $exclude_default_token,
		);

		return apply_filters( 'automator_option_' . strtolower( $option_code ) . '_field', $option, $args );

	}


Scroll to Top