[SOLVED] ACF Repeater + Condition | Show/Hide Bricks Element if ACF field is empty or not

On a blog single template inside an ACF repeater I need to show/hide a button, depending on whether an ACF text field is empty or not. Button should SHOW if acf text field has a value. Button should HIDE if acf text field is empty.

Here is what I would like to achieve:

My button has this class:
.b-blog-content__button

My ACF text field has this name/ID:
blog_-rep-_button_text

I have tried the code here, but it doesn’t seem to work inside of an ACF Repeater:

Anyone of the pro coders here willing to assist?
@Sridhar => This would be a tutorial request for BricksLabs too :slight_smile:

Share the code that you’re using and the elements structure.

I have been trying this code, but it doesn’t work:

add_filter( ‘bricks/element/render’, function( $render, $element ) {

// Get the element CSS classes
$classes = $element->attributes['_root']['class'];
  // Check if there is at least one class
$classes = ! empty( $classes ) ? $classes : true;
// Show or hide "acf repeater blog content button" if "acf button text" is or isn't empty
//btn-empty-check
   if ( $classes && in_array( 'btn-empty-check', $classes ) )
   {
            $textvalueinfo = $element->settings['text'];
            //print_r($element->settings);
            if(!empty($textvalueinfo))
            {
                 return $render;
            }
            
   }else
   {
       return $render;
   }

}, 10, 2 );

And this is my elements strucutre:

Do you need any other information?

Nope, seems clear to me. Try this code, but change the field name from “acf_button_text_field_name” to the one you’re using:

add_filter( ‘bricks/element/render’, function( $render, $element ) {
   // Get the element CSS classes
   $classes = $element->attributes['_root']['class'];
   // Check if there is at least one class
   $classes = ! empty( $classes ) ? $classes : true;
   // Show or hide "acf repeater blog content button" if "acf button text" is or isn't empty
   //btn-empty-check
   if ( $classes && in_array( 'btn-empty-check', $classes ) )
   {
            // REPLACE THE NAME IN THE FOLLOWING LINE OF CODE
            // Get the corresponding field value    
            $acf_btn_text = get_field('acf_button_text_field_name');
            // Check if it is empty
            $is_btn_text_empty = empty($acf_btn_text);

            // If it is empty - don't render (return false) and if it isn't - render (return true)
            if($is_btn_text_empty)
            {
                 return false;
            } else{
                 return true;
            
   } else
   {
       return $render;
   }
}, 10, 2 );

It seems like you based your code on the one provided by myself in the mentioned forum post. It is a bit simplified, just not to create additional variables. This might be confusing, so here I’ve done it in a more “plain” way. The way that render filter works is that it return “true” or “false” value for each element. What you were doing, was returning the initial value of $render without changing it. And this is “true” by default. You also don’t have to take the text value from the settings of en element, because it should work by calling the ACF field itself with get_field.

2 Likes

Thank you … only on the phone at the moment … will check a bit later!

Thanks for sharing that Matiasko. But am afraid that is not going to work for what beziehungsweise is trying to do.

2 Likes

@beziehungsweise See Example 6 at Filter: bricks/element/render – Bricks Academy.

1 Like

Oh right, I forgot this is how it’s done with repeater’s subfields. Thanks for correcting me and providing the link :slight_smile:

Thanks a lot @Sridhar. This is working perfectly fine! Great!!!

It will be better if we have an option in the widget like elementor, so we won’t need to activate the conditions for that
image

4 Likes