|
/ Documentation / Filter: srfm_block_field_markup

Filter: srfm_block_field_markup

Description

The srfm_block_field_markup filter allows you to modify the final HTML markup of a form field before it is rendered on the page. This is a powerful filter that can be used to make significant changes to a field’s appearance or functionality, including adding, removing, or changing dropdown options.

The filter is applied at the end of the markup() method in the SRFM\Inc\Fields classes (e.g., Dropdown_Markup, Text_Markup, etc.).

Parameters

  • markup (string): The complete HTML markup for the field.
  • args (array): An array of arguments containing details about the field:
    • slug (string): The field type slug (e.g., dropdown, text).
    • field_name (string): The generated name of the field.
    • is_editing (bool): Whether the field is being rendered in the entry editor.
    • attributes (array): An array of the block’s attributes, including the block_id.

Return

  • (string): The modified HTML markup.

Example: Adding a New Option to a Dropdown

This example shows how to use the srfm_block_field_markup filter to add a new “Student” option to a specific dropdown field.

phpCopyEditadd_filter( 'srfm_block_field_markup', 'add_custom_dropdown_option', 10, 2 );

/**
 * Adds a new "Student" option to a specific dropdown field.
 *
 * @param string $markup The original HTML markup of the field.
 * @param array  $args   An array of arguments about the field.
 * @return string The modified HTML markup.
 */
function add_custom_dropdown_option( $markup, $args ) {
    // Target a specific dropdown field. Replace 'your-block-id' with the actual block_id.
    $target_block_id = 'your-block-id';

    // Ensure we are modifying the correct dropdown field and not in the admin entry editor.
    if ( 'dropdown' === $args['slug'] && isset( $args['attributes']['block_id'] ) && $target_block_id === $args['attributes']['block_id'] && ! $args['is_editing'] ) {

        // Define the new option to be added.
        $new_option = '<option value="Student">Student</option>';

        // Find the placeholder option to insert the new option after it.
        $placeholder_text = ! empty( $args['attributes']['placeholder'] ) ? $args['attributes']['placeholder'] : 'Select an option';
        $placeholder_tag = '<option class="srfm-dropdown-placeholder" value="" disabled selected>' . esc_html( $placeholder_text ) . '</option>';
        
        // Insert the new option after the placeholder.
        $markup = str_replace( $placeholder_tag, $placeholder_tag . $new_option, $markup );
    }

    return $markup;
}

Replace 'your-block-id' with the actual block ID of the dropdown field. You can find this ID in the block’s Advanced settings in the editor.

Filter: srfm_field_config (For JavaScript)

As stated previously, srfm_field_config cannot change dropdown options, but it is useful for other things.

Description

The srfm_field_config filter allows you to add custom configuration data to a form field’s data-field-config attribute. This attribute is rendered in the field’s HTML markup and can be accessed by JavaScript on the frontend. It is useful for custom client-side logic, such as calculations, validations, or conditional logic.

Parameters

  • field_config (array): The field configuration array. This is initially an empty array that you can add your custom data to.
  • attributes (array): An array of the block’s attributes.

Return

  • (array): The modified field configuration array.
Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
On this page
Scroll to Top