Custom Form Actions

On form custom actions, I understand I have to write a bricks filter to capture the form and process how I want which isn’t an issue. My issue is can I write multiple custom actions, and then how do I declare on the form which action I want to run?

It appears all I can do is select “Custom” on the form action menu, but then nothing new appears, I guess I was expecting to declare my function name or something? Am I maybe missing something here?

Hey,

Yeah, you’re missing a step. Setting custom really does nothing more than turn on the custom action hook. You’ll need to define an action somewhere in php like this.

<?php 
function my_form_custom_action( $form ) {  
  // $fields = $form->get_fields();
  // $formId = $fields['formId'];
  // $postId = $fields['postId'];
  // $settings = $form->get_settings();
  // $files = $form->get_uploaded_files();
  
  // Perform some logic here...

  // Set result in case it fails
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', //or danger or info
    'message' => esc_html__('Oh my custom action failed', 'bricks'),
  ]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

More can be found on the academy page:

@cmstew

Thanks for the quick reply, and that example action I had seen. I’m trying to foreshadow a little bit, and I’m thinking I’m going to have a few forms that will need differing custom actions applied to each form separately.

Can I pass which function I’m looking to run from the form, or do I need to access the one action and then determine which form has been submitted to determine which process to follow from there?

Thanks for any guidance…

Hey!

That’s a great question. Well, it might not be the most elegant solution but you could add a hidden field to your forms called custom-action, or whatever you want, and then you can define which custom action you want to trigger on that form by assigning the value. Then in your function, you could check the fields first before doing your action:

<?php 
function my_form_custom_action( $form ) {  
  $settings = $form->get_settings();

  foreach ($settings['fields'] as $field) {
    if (isset($field['label']) && $field['label'] == 'custom-action') {
      if ( $field['value'] == 'action-1') {
         // Perform some logic here...
      }
      if ( $field['value'] == 'action-2') {
         // Perform some other logic here...
      }
    }
  }
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

Hey @cmstew you think you could help here:

Thanks.