Hi Developers,
I have set up an ACF-style Gutenberg block with a couple of select fields in it. The second field is populated conditionally on the output of the first, ie, the first field is a list of posts of post type “Resource” and the second field is a list of documents attached to the Resource posts. If I choose a resource, the list of documents attached to that resource is populated into the second select box.
I’ve used the approach in this support thread to do it:
https://support.advancedcustomfields.com/forums/topic/javascript-api-for-fields-in-block-edit-mode/
The second field is populating fine now via an ajax call. The problem is, I want to set the “selected” value in the second dropdown to the current value of the ACF field. To do that I need to know what the current value is.
I’ve tried using a lot of variations on
get_field( $post_id, $selector)
and
acf_maybe_get_field( $selector, $post_id );
which is what ACF is using to get the value. This doesn’t get me the value, I think because my plugin is outside the scope of the Gutenberg block environment… (ACF is doing some funky stuff with getting those values)
Can anyone please help me to get the current value of the ACF field for the specific block? I don’t mind if it’s in PHP or in the JS API – I can use either.
Thanks!
I also wanted to get meta data values in a block, and after trying everything, and eventually opening a ticket about it, and the simple answer I got was “it is not possible”.
This should have been a hint, but I tried anyways: – https://www.advancedcustomfields.com/resources/blocks/
Under the heading “Anywhere and Everywhere”, it says that “ACF blocks are not tied to metadata…”. I knew this going in, but I had to “force” a solution, needless to say, it was a total fail.
Does this help? https://support.advancedcustomfields.com/forums/topic/acf-5-8-parse-gutenberg-blocks-and-get-acf-data-outside-of-post/
Essentially you load the post content, parse it for blocks then when you find an ACF block you can use get_field
.
You can get acf custom field’s value from block’s data.
function get_block_data($post, $block_name = 'core/heading', $field_name = "" ){
$content = "";
if ( has_blocks( $post->post_content ) && !empty($field_name )) {
$blocks = parse_blocks( $post->post_content );
foreach($blocks as $block){
if ( $block['blockName'] === $block_name ) {
if(isset($block["attrs"]["data"][$field_name ])){
$content = $block["attrs"]["data"][$field_name ];
}
}
}
}
return $content;
}
Try this, you can only get the custom block section array.
function find_from_blocks($blocks) {
$list = array();
foreach ( $blocks as $block ) {
if ( 'acf/profit' === $block['blockName'] ) {
// add current item, if it's a heading block
$list[] = $block;
} elseif ( ! empty( $block['innerBlocks'] ) ) {
// or call the function recursively, to find heading blocks in inner blocks
$list = array_merge( $list, find_from_blocks( $block['innerBlocks'] ) );
}
}
return $list;
}
$post = get_post();
$blocks = parse_blocks( $post->post_content );
$acf_block = find_from_blocks($blocks);
$acf_data = $acf_block[0]['attrs']['data'];
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.