Data Attribute-based Conditional Output in Bricks

In the Bricks Facebook group a user asks:

Data Attribute as a Condition

Is it possible to place a data attribute as an element condition, if yes, how?

Before Bricks added the ability to set conditions on elements in the editor, we’d use bricks/element/render filter for the conditional output.

This Pro tutorial shows how this filter can be used to not render elements that have a specific data attribute with a name of data-dont-output set.

On the front end:

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

add_filter('bricks/element/render', function($render, $element) {
    // Check if the element has attributes set
    if ( isset( $element->settings['_attributes'] ) && is_array( $element->settings['_attributes'] ) ) {
        // Loop through the attributes
        foreach ( $element->settings['_attributes'] as $attribute ) {
            // Check if the attribute name is 'data-dont-output'
            if ( isset( $attribute['name'] ) && $attribute['name'] === 'data-dont-output' ) {
                // If found, return false to prevent rendering
                return false;
            }
        }
    }
    
    // If the attribute is not found, return the original render value
    return $render;
}, 10, 2);

FYI → Screenshot of the $element object of an element that has this attribute set:

The full object: https://pastebin.com/raw/XGbyTmRe