SOLVED: Custom Action BricksBuilder Form Bug

Bricks Version: 1.1.3
Browser: Chrome 90
OS: macOS / Windows / Linux / etc.
URL: (a link to a page that illustrates the issue would be really helpful)

I am currently experiencing an issue with the custom action on my form. The custom action is supposed to trigger the execution of a PHP file which creates a new anchor element using the following code: echo '<a href="'.$new_file_path.'" download="'.$new_file_name.'">Download HTML</a>';
However, the anchor element is not being created and only the success message from the custom action is displayed. It appears that the form is preventing the creation of the element.

Additionally, an error message is being logged:

Fatal error: Uncaught Error: Using $this when not in object context in /home/u406562983/domains/love4quality.com/public_html/wp-content/themes/bricks/searchform.php:4 Stack trace: #0 /home/u406562983/domains/love4quality.com/public_html/wp-includes/general-template.php(308): require() #1 /home/u406562983/domains/love4quality.com/public_html/wp-content/themes/bricks/404.php(11): get_search_form(Array) #2 /home/u406562983/domains/love4quality.com/public_html/wp-includes/template-loader.php(106): include(‘/home/u40656298…’) #3 /home/u406562983/domains/love4quality.com/public_html/wp-blog-header.php(19): require_once(‘/home/u40656298…’) #4 /home/u406562983/domains/love4quality.com/public_html/index.php(17): require(‘/home/u40656298…’) #5 {main} thrown in /home/u406562983/domains/love4quality.com/public_html/wp-content/themes/bricks/searchform.php on line 4

I am unsure if this error message is related to the aforementioned problem.

Yeah, this probably won’t work. The custom action is one way. So without a page reload and a cookie or URL param of some sort, you won’t get the results you’re expecting.

What you’ll need to do is hook into the ajax success and trigger your code in JavaScript.

1 Like

Thank you. I must say that’s some weird behavior, I hope they add this feature soon

Don’t despair though. Maybe someone else will have a better answer. :wink:

I’ll take a closer look this week and verify.

Hi @DennisTheMenace ,

The form custom action is allow you to perform some backend action through bricks/form/custom_action hook.

You are not able to add a new element or new DOM in the front end.
However, you might be able to output the URL in the result message.

 $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', //or danger or info
    'message' => 'https://mydomain.com/abc.pdf', // Your URL here
  ]);

Regarding the fatal error, it should be solved in Bricks 1.6.2
Please can you check if you have updated to 1.6.2?

Regards,
Jenn

Hi Jenn @itchycode ,

Thank you for reaching out to me. I just updated to Bricks 1.6.2 and the fatal error seems to be resolved. However, I am still having an issue with the $form->set_result method. I am unable to get it to work properly as it always displays the message that is set via the page builder. Even if I leave the message blank in the page builder, the message only displays “Success” and it doesn’t seem like the code is able to overwrite the success message. Is there anything else I can try or any other solution to this issue?

Thank you for your help.

Hi @DennisTheMenace ,

You are right, the action hook unable to overwrite the success message.
I have recorded this into the bug tracker.

As a workaround, please use this filter together bricks/form/response

// This function have to set a result in the end
function my_form_custom_action( $form ) {  
  $fields = $form->get_fields();
  $formId = $fields['formId'];

  if( $formId !== 'qdjeeb' ) {
    return;
  }

  // Perform some logic here...

  // Set result in case it fails
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', 
    'message' => 'https://google.com',
  ]);

}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

// This function is going to change the response if the custom action is set and if it's a success
function my_custom_response( $response, $form ) {
  $results = $form->get_results();

  // Loop through success results and check for custom action
  if( isset( $results['success']) ) {
    foreach( $results['success'] as $result) {
      if ( $result['action'] === 'my_custom_action' ) {
        $response['message'] = $result['message'];
        break;
      }
    }
  }

  return $response;
}

add_filter( 'bricks/form/response', 'my_custom_response', 10, 2 );

If this is working good for you, I will add this to our academy example as well.

Looking forward to your feedback.

Regards,
Jenn

2 Likes

Thanks for the help, @itchycode! Your workaround worked great. Appreciate your help.

1 Like

@DennisTheMenace

Just want to update that this issue should already been solved in 1.7.1 version.
I will mark this thread as SOLVED.
If you still encounter issue please let me know.

Thanks!

Regards,
Jenn