Support

Account

Home Forums Gutenberg ACF Blocks – Need stable id Reply To: ACF Blocks – Need stable id

  • For some reason, this is what I got working, at least for now:

    block.json

    {
        "name": "acf/clb-custom-info-card",
        "title": "Info Card",
        "description": "A custom info card block.",
        "style": [ "file:./clb_custom_info_card.css" ],
        "category": "common",
        "icon": "admin-comments",
        "keywords": ["testimonial", "quote"],
        "acf": {
            "mode": "preview",
            "renderTemplate": "clb_custom_info_card.php"
        },
        "align": "full",
        "providesContext": {"acf/fields": "data"},
        "attributes": {
            "clb_custom_id": {
                "type": "string",
                "default": "1234abcd"
             }
          }
    }

    And then my acf/pre_save_block filter:

    
    add_filter(
        'acf/pre_save_block',
        function( $attributes ) {
    
            //error_log('attributes');
            error_log( print_r( $attributes, true ) );
    
            // if ( empty( $attributes['clb_custom_id'] ) ) {
            //     $attributes['clb_custom_id'] = 'clb_custom_id-' . uniqid();
            // }
    
            if( !$attributes['clb_custom_id'] ) { $attributes['clb_custom_id'] = uniqid(); }
    
            // if ( empty( $attributes['data']['clb_custom_id'] ) ) {
            //     $attributes['data']['clb_custom_id'] = 'clb_custom_id-' . uniqid();
            // }
    
            return $attributes;
        }
    );