Support

Account

Home Forums ACF PRO check if block instance already exists

Solved

check if block instance already exists

  • I’m trying to populate the fields of a block with some default values when the block is new.

    I’m doing this with the acf/pre_render_fields filter, so I’m trying to do something like this:

    add_filter( 'acf/pre_render_fields', function( $fields, $post_id ) {
    
      if(is_new_block($post_id)) {
        // add some default values to the $fields array
      }
    
      $return fields;
    
    }, 10, 2

    Obviously the is_new_block() function does not exist. But, how can I detect whether a block has just been created or whether it’s a block that already existed before?

  • Hey @julescolle – if I am heading down the right path for you, you merely want to set default values for fields (if no value is entered yet)?

    One way is that you can set default values right from the get_field() without any conditional logic snippets.

    $example = get_field( 'cpar_example' ) ?: 'use this value if not entered';

    This would then display block preview with those default values when first added to the editor and/or preview. Then when entering a value into the fields, it would override.

    Hope this helps…

  • Hi @carlparkerwp that’s correct, but it’s a bit more complex than that.
    I’m trying to update the entire $fields array with default values (which I’m pulling from a block in another post) at once, and I want to make sure the edit-form fields are populated correctly as well.

    I actually found my solution after some trial and error. It turns out that in the acf/pre_render_fields filter get_fields() will return false if the current block was not saved to the database previously.

    So, this works for my case:

    
    add_filter( 'acf/pre_render_fields', function( $fields, $post_id ) {
    
      if(!get_fields($post_id)) {
        // add some default values to the $fields array
      }
    
      $return fields;
    
    }, 10, 2
    

    Actually, I’m doing some additional checks to see If I’m actually loading field blocks, so my actual code is this:

    
    add_filter( 'acf/pre_render_fields', function( $fields, $post_id ) {
    
        if (
            // check if we are fetching a block
            ($_POST['action'] ?? '') !== 'acf/ajax/fetch-block' ||
            // check if we are loading the block's edit-form (not the preview)
            ($_POST['query']['form'] ?? '') !== 'true' ||
            // check if the block is a new block
            get_fields() !== false
        ) {
            return $fields;
        }
    
        $fields = populate_fields_with_default_data($fields, $fields[0]['parent']);
    
        return $fields;
    }, 20, 2 );
    

    For brevity I haven’t included the entire populate_fields_with_default_data() function. This is a pretty complicated custom function that I have written for my own use case, so I don’t think others will benefit from this 🙂

  • My message seems to have disappeared, so here it is again

    Hi @carlparkerwp that’s correct, but it’s a bit more complex than that.
    I’m trying to update the entire $fields array with default values (which I’m pulling from a block in another post) at once, and I want to make sure the edit-form fields are populated correctly as well.

    I actually found my solution after some trial and error. It turns out that in the acf/pre_render_fields filter get_fields() will return false if the current block was not saved to the database previously.

    So, this works for my case:

    
    add_filter( 'acf/pre_render_fields', function( $fields, $post_id ) {
    
      if(!get_fields($post_id)) {
        // add some default values to the $fields array
      }
    
      $return fields;
    
    }, 10, 2
    

    Actually, I’m doing some additional checks to see If I’m actually loading field blocks, so my actual code is this:

    
    add_filter( 'acf/pre_render_fields', function( $fields, $post_id ) {
    
        if (
            // check if we are fetching a block
            ($_POST['action'] ?? '') !== 'acf/ajax/fetch-block' ||
            // check if we are loading the block's edit-form (not the preview)
            ($_POST['query']['form'] ?? '') !== 'true' ||
            // check if the block is a new block
            get_fields() !== false
        ) {
            return $fields;
        }
    
        $fields = populate_fields_with_default_data($fields, $fields[0]['parent']);
    
        return $fields;
    }, 20, 2 );
    

    For brevity I haven’t included the entire populate_fields_with_default_data() function. This is a pretty complicated custom function that I have written for my own use case, so I don’t think others will benefit from this 🙂

  • Right on @julescolle — glad you were able to find things out man and I have been noticing disappearing messages too 🙁

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.