SOLVED: Conditions do not work on global elements

I wonder if somebody wouldn’t mind helping me with a problem, please.

I’m using Bricks and Metabox.

I have two buttons on a page. I’d like to hide one button if the Metabox field is empty and another to hide if the Metabox field is not empty. The Metabox field type is a URL field. The reason for the 2 buttons/links is that I have a button to take users to a contact form. I’ve used it as place holders my services page while I build out each service page. Each service is in a query loop. As I build out each page, I’ll swap to the second button to take the user to the service page.

I can hide the button just fine if the Metabox field is empty using the conditions in Bricks.

Would anyone know how to hide an element in bricks if a Metabox field is populated by any chance?

I edited the post and changed it to a possible bug. @timmse

I used a global button that didn’t work but worked fine when I added the plain button. The condition below button only shows if the button is blank below.

Hi,
Thanks so much for your report!

I’m sorry, but what exactly is the bug and what do I need to do, to reproduce it?
I can’t see the forest for the trees :laughing:

Best regards,
timmse

Hello @timmse

When I use the conditions in Bricks to hide and show a button based on if the Metabox field is empty or not.

It works fine if I use a button element but not if I set the button as a global element.

Let me know what else I can provide to help.

Kind regards

Stephen

Hey,
Thanks for clarifying :raised_hands:

I’ve added it to the bug tracker as the element conditions should work on global elements as well.

Best regards,
timmse

1 Like

Hey @timmse

Is there any PHP I can use as a stop gap by any chance?

Kind regards

Stpehen

No, unfortunately not. We’ll provide the fix as soon as possible.

I might oversee some details of this concrete scenario but I think I found a way to make it work for global elements using the bricks/element/render hook. So this might be a temporary workaround:

add_filter( 'bricks/element/render', function( $render, $element ) {
    /*
     * Print (global) element ID for global elements.
     * Use this if statement to find your global element's (global) ID.
     * Remove or comment out this if statement after finding the correct ID!
     */
    if ( $global_element_id = $element->element['global'] ) {
        printf( '<pre>Global Element ID: %s</pre>', $global_element_id );
    }
    
    /*
     * Replace 'cyefwp' with your global element ID
     * and use whatever custom logic to output the
     * element or not.
     */
    if ( $element->element['global'] === 'cyefwp' ) {
        return ! empty( rwmb_meta( 'global_button_url' ) );
    }
    return $render;
}, 10, 2 );

CleanShot 2023-02-09 at 12.20.05

2 Likes

@aslotta

Your code works. You are a genius and thanks very much. :grinning: :clap:

1 Like

Unfortunately my code editor throw up an error so I’ve decided to create a template with the two buttons.

Hi guys,
We’ve fixed this bug in Bricks 1.7 beta, now available as a manual download in your Bricks account: Account – Bricks

Please let us know if you are still experiencing issues.

Best regards,
timmse

Hello @timmse

No joy, I’m afraid.

I had three buttons. 2 were the standard, and the third was the global button.

I have all four fields not empty.

Condition showing as not empty.

Result on the front end. - This is as expected. :grinning:

Condition showing as empty.

The two standard buttons disappeared as expected but not the global button. :frowning_face:

The global button appears even if I set the condition to not empty or empty and the opposite. I wondering if it’s worth someone else also testing this too.

Hey,
Ok, then it’s related to the loop. Can you confirm it works as expected by turning off the loop?

Hello @timmse

No joy, I’m afraid. All three have the same conditions. The metabox field is populated.

Condition showing as empty.

The result - Global button is showing

That’s strange. Would you be so kind as to send temporary login credentials, information about where to find the issue, and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase?

1 Like

Hello @timmse

I wanted to say a big thank you for fixing the issue. :grinning:

You, the man!!

I’m happy for this bug to be marked as resolved.

Many thanks

I’m glad I could help. Get in touch if the problem occurs again :slight_smile: