Hiya
I have been following the code examples on:
https://github.com/WordPress/gutenberg/blob/master/docs/templates.md
To be able to add additional blocks to forma block template. Is there any way to integrate an ACF block into these templates?
My code so far is:
function my_add_template_to_posts() {
$post_type_object = get_post_type_object( 'page' );
$post_type_object->template = array(
array( 'core/paragraph', array(
'placeholder' => 'Add a root-level paragraph',
) ),
array( 'core/columns', array(), array(
array( 'core/column', array(), array(
array( 'core/image', array() ),
) ),
array( 'core/column', array(), array(
array( 'core/paragraph', array(
'placeholder' => 'Add a inner paragraph'
) ),
) ),
) )
);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'my_add_template_to_posts' );
But not sure how to reference a pre made ACF block?
Any help would be grateful
TIA
Tom
Hi Tom,
I was expirimenting with the same thing as i couldnt find information about this.
But for example:
First You create a custom block via acf_register_block:
acf_register_block(
array(
'name' => 'blockname',
'title' => __('title'),
'description' => __('description')
));
Then you can take the name of the acf_block ‘blockname’ and use it as following in your post type template:
$post_type_object->template = array(
array( 'acf/blockname', array() ),
);
Note: If your blockname contains a space or a underscore, acf renames this to a dash, so ‘block_name’ becomes ‘block-name’.
Hope this helps!
Taking this a bit further, is there a way to add default values in a template?
In the wordpress documentation I see that you can add for some core blocks a placeholder.
https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
Is there a way to add default content in acf blocks when you define them in the block template?
I second kkoyan’s question. Adding Gutenberg template blocks where the ACF block contents are preselected would be appreciated.
+1
I was expecting something like this to work, but computer says no …
array( ‘acf/blockname’, array(
‘header’ => __( ‘You may also like’, ‘td’ ),
) ),
where header is the name of the field.
+1
It would be very nice to predefine values for blocks in templates.
+1
It’d saves plenty of times, and give us the real power to use acf blocks as template
Hey Guys,
This is possible already.
Here is the post about it:
https://support.advancedcustomfields.com/forums/topic/programmatically-set-blocks-initial-value/
And the code which helps you to make better experiences! : )
The magic is that, you need to wrap them with a data []
'template' => [
['acf/my-block', [
'data' => [
'field_name' => 'the value you want to see by default',
]
]]
];
I hope it helps you too as well!
I’m stuck with repeater subfields now.
Didn’t find a way to fill them yet.
If any of you able to do that, don’t hesitate to write here about that. 😉
Bests,
Christian
Agreed, three cheers to @ChrisDark for this. I searched high and low for this data[] tag to set my custom field values. I have to populated cloned field values…did you ever figure out how to tap into a subfield (which I suspect would use similar syntax)?
In case anyone is still looking for a solution to adding repeater fields as default template:
$template = [
['acf/acf-block-name', [
'data' => [
'repeater_field_name_0_title' => 'Your title here',
'repeater_field_name_0_text' => 'Your text here',
'repeater_field_name_0_button' => [
'title' => 'Button title',
'url' => 'Button URL',
'target' => 'Button URL'
],
'repeater_field_name' => 1,
]
]],
];
I hope this will help someone. 🙂
In addition to the solution I posted above, I came across the problem that the ACF blocks weren’t being stored properly. The ACF field ID’s are required to make sure the entire template is correctly saved into the DB.
$template = [
['acf/acf-block-name', [
'data' => [
'repeater_field_name_0_title' => 'Your title here',
'_repeater_field_name_0_title' => 'field_abc1234567890',
'repeater_field_name_0_text' => 'Your text here',
'_repeater_field_name_0_text' => 'field_abc1234567890',
'repeater_field_name_0_button' => [
'title' => 'Button title',
'url' => 'Button URL',
'target' => 'Button URL'
],
'_repeater_field_name_0_button' => 'field_abc1234567890',
'repeater_field_name' => 1, // the number of set repeater fields
'_repeater_field_name' => 'field_abc1234567890'
]
]],
];
The topic ‘Adding ACF blocks to a block template’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.