Support

Account

Home Forums Gutenberg Programmatically set blocks initial value

Solved

Programmatically set blocks initial value

  • Hi all,

    My use case is the following:
    I have custom post type and custom ACF Gutenberg blocks registered with it.
    I want when new post from this posttype is created, the Gutenberg editor to open with some default blocks in it. It is possible via the template option when creating the post type.

    Here is what it looks like:

    
    function demo_register_book_post_type() {
      $args = array(
          'public' => true,
          'label'  => 'Demo',
          'show_in_rest' => true,
          'template' => array(
              array( 'acf/custom-block-1', array(
                  'align' => 'left',
              ) ),
              array( 'acf/custom-block-2', array(
                  'placeholder' => 'Some placeholder...',
              ) ),
          ),
      );
      register_post_type( 'demo', $args );
    }
    add_action( 'init', 'demo_register_book_post_type' );
    

    My problem is that I could not fill set initial values to these blocks. If I have field ( ex. name ) I want to set default name to the blocks. Doing something like:

    'template' => array(
      array( 'acf/custom-block-1', array(
          'name' => 'Initial name',
      ) ),
    ),

    does not work and results with JS error Uncaught TypeError: Cannot read property ‘preview’ of undefined coming from acf-pro-blocks.min.js file.

    Any way to set values of these blocks?

    Any help appreciated.

  • Ok, my bad, I was stupid:

    The options should be wrapped in data array:

    'template' => [
         ['acf/my-block', [
           'data' => [
            'name' => 'default name',
           ]
         ]]
    ];

    I will still leave the post here so anyone with similar problem could benefit from it.

  • Hey @panayotoff ,

    Thank you for your share, it helped me a lot!

    Did you able to fill up repeater type of fields too?
    It seems not to work for me with those type of fields.

  • @chrisdark have you found out how to make this work with repeater subfields?

    It seems that only top-level ACF fields appear in a template when you create a new page. Any subfields will not show in the editor – at least not initially.

    I used icons_0_custom_icon to generate the first row of the repeater. icons is the name of the repeater, 0 refers to the first row and custom_icon is the name of the subfield.

    It does work, just not initially. At first, the preset subfield data does not appear in the block editor. However, if you click on the block, the inspector/sidebar shows the correct data and it only appears in the editor if you make any sort of adjustment to the data in the inspector.

    Wish there was a way that worked properly. :/

  • Hello. This method works only top level fields. Not working on repeater and sub fields.

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

You must be logged in to reply to this topic.