Site logo

[Support request] Adding conditions to remove a container block through a filter

Home Forums Support [Support request] Adding conditions to remove a container block through a filter

Home Forums Support Adding conditions to remove a container block through a filter

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2415154
    Sam

    Hi,

    I’ve looked through the code around how the flag gpRemoveContainerCondition in the $block[‘attrs’] goes about removing a container where there is no post meta, and was looking to filter/extend this in the same way that the filters like generate_dynamic_element_XXX modify the text.

    My use case is that I have a Generate Blocks headline displaying the term meta on an element of a taxonomy term page, and I’d like to hide the container it’s in if the term meta isn’t present.

    Is there any way to achieve this through a filter of the block that I’ve not noticed?

    Thanks,
    Sam

    #2415200
    David
    Staff
    Customer Support

    Hi there,

    you could use the core render_block filter to remove the $block_content for example:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    
        if ( 
            !is_admin()
            && is_category()
            && ! empty( $block['attrs']['className'] ) 
            && 'your-custom-class' === $block['attrs']['className']  
        ) {
    		
            $block_content = null;
    
        }
    
        return $block_content;
    
    }, 10, 2 );
    #2415601
    Sam

    Hi David,

    Thanks – that looks really promising and like it should achieve what I need.

    I’ve just tried it, and it looks like the conditional is evaluating false on is_category() (I have removed the two last conditions to see if it was an issue with class name). The element’s display rule is set to render on a custom taxonomy archive that is rendering a WooCommerce template – would this have an effect on this working?

    I can’t remove the is_category() check without triggering a critical error, so am not sure where to go from here to diagnose.

    For what it’s worth, I also noticed that GenerateBlocks is having an issue in the element on the archive page – it’s not rendering a headline with dynamic content where there are no posts in the loop of the archive page – not sure if it’s related to this issue, but just thought I’d mention as that’s two issues on the same archive.

    Thanks in advance for any light you can shed – and sorry for coming back to you on what should have been an easy answer!

    #2415733
    Fernando
    Customer Support

    Hi Sam,

    render_block() should work anywhere even on Custom Taxonomy archive pages. Try replacing is_category() with is_tax().

    David’s code is just an example of its usage. It needs modifications for it to work as you would prefer.

    Can you provide admin login credentials so we can have a better grasp of the concept?

    Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2415967
    Sam

    Ah, all sorted now – thanks for coming back to me. Turns out it needs to return a blank string rather than null or else a core function trips out:

    [15-Nov-2022 07:16:24 UTC] PHP Fatal error: Uncaught TypeError: Argument 1 passed to Automattic\WooCommerce\Blocks\BlockTypes\ClassicTemplate::add_alignment_class_to_wrapper() must be of the type string, null given, called in /home/wp-includes/class-wp-hook.php on line 310 and defined in /home/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTypes/ClassicTemplate.php:233

    Still need to figure out the weird GB dynamic data thing, but looks like that’s now not for this thread.

    Thanks for your help with this.

    #2415971
    Fernando
    Customer Support

    I see. You’re welcome Sam! Feel free to raise a new topic if you have any further issues.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.