AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
835 stars 171 forks source link

Passing data values through block.json #741

Closed cabrailsford closed 1 year ago

cabrailsford commented 1 year ago

With the previous way of registering ACF blocks, we could pass data through a data array in the attributes, which in turn could be used in the block template or callback, either with $block['data'] or get_field(). What is the way to do this with using block.json, or is it even possible now?

CreativeDive commented 1 year ago

@cabrailsford is this helping you here? https://www.advancedcustomfields.com/resources/whats-new-with-acf-blocks-in-acf-6/#block-context-changes

Using "providesContext": {"acf/fields": "data"} you can pass ACF field data and using "usesContext": ["acf/fields"], to get the field data inside another block.

cabrailsford commented 1 year ago

Thanks @CreativeDive, but I don't think that's exactly what I'm needing. Rather, I have a single block that has 1 ACF field, and I want to pass a default to the field using the data attribute, but unsure how with block.json. I could do it with the old method as an array, but not sure how that translates (if at all) to attributes in block.json

CreativeDive commented 1 year ago

@cabrailsford Ahh, something like:

"acf": {
        "mode": "preview",
        "renderTemplate": "index.php",
    "data": {
            "field_name_1": "value_1",
            "field_name_2": "value_2"
        }
    },

Not sure if that works in block.json @lgladdy? Just tested it but no success.

CreativeDive commented 1 year ago

@cabrailsford tested something and in the end it works like this:

"attributes": {
        "alignText": {
            "type": "string",
            "default": "center"
        },
        "data": {
            "type": "object",
            "default": {
                "field_name": "field_value",
                "field_name2": "field_value",
                "field_name3": "field_value"
            }   
        }

    },

Cheers!

cabrailsford commented 1 year ago

@CreativeDive Sorry for the slow reply, but this seems to work! Between that and the usesContext, I've got most things working as needed!