ACF Blocks: Initial Gutenberg Preview Empty Despite Default Field Values (Timber + render_callback)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Bắt đầu với GutenbergHelper.php, đặc biệt là acfRenderCallback(), sau đó truy vết việc đăng ký từ BlockLinkTiles/functions.php và dữ liệu mà BlockLinkTiles/index.twig mong đợi. Tái hiện lần chèn đầu tiên trong trình chỉnh sửa Gutenberg và xác định đường dẫn giá trị mặc định được hỗ trợ; được xem là hoàn tất khi lần render ban đầu nhận các giá trị đã cấu hình mà không cần tương tác với trường và các lần render sau vẫn chính xác.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
When registering a custom ACF block using acf_register_block_type() with a render_callback, the Gutenberg editor preview is blank on the very first load after inserting the block — even though default field values are defined. The data object passed to render_callback is empty on this initial render. As soon as any field value is changed in the inspector controls, the preview updates correctly and continues to work from that point on.
To Reproduce
- Register a custom ACF block using
acf_register_block_type()with arender_callback - Define default values for the block's fields in ACF
- Open a page or post in the Gutenberg editor
- Insert the custom block for the first time
- Observe that the block preview is blank — the
render_callbackreceives an empty data object
Expected behavior
On initial insertion of the block into the editor, the render_callback should receive the defined default field values and render an accurate preview immediately, without requiring any user interaction.
Screenshots or Video
https://www.youtube.com/watch?v=ldLwg3o8PPY
Code
We use a central GutenbergHelper class to register and render all ACF blocks for Gutenberg.
GutenbergHelper.php — handles block registration and the shared render_callback:
<?php
namespace Flynt\Utils;
use ACFComposer\ACFComposer;
use Flynt\ComponentManager;
use Timber\Timber;
class GutenbergHelper
{
public static function acfRenderCallback($block, $content, $is_preview = false, $post_id = 0)
{
$componentName = str_replace('acf/', '', $block['name']);
$componentManager = ComponentManager::getInstance();
$filePath = $componentManager->getComponentFilePath($block['filePath'], 'index.twig');
$relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/');
if (!is_file($filePath)) {
trigger_error("Template not found: {$filePath}", E_USER_WARNING);
return '';
}
$fields = \get_fields() ?? [];
$fields['is_preview'] = $is_preview;
$fields = apply_filters(
"Flynt/addComponentData?name={$componentName}",
$fields,
$componentName
);
$fields['block'] = $block;
Timber::render($relativeFilePath, $fields);
}
public static function registerGutenbergBlock($component)
{
ACFComposer::registerFieldGroup([
'name' => $component['name'],
'title' => $component['label'],
'fields' => $component['sub_fields'] ?? [],
'location' => array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'acf/' . strtolower($component['name']),
),
),
),
]);
if (function_exists('acf_register_block_type')) {
$componentName = $component['name'];
$componentLabel = $component['label'] ?? $componentName;
$gutenbergConfig = self::getComponentGutenbergConfig($componentName);
$blockConfig = array(
'name' => $componentName,
'title' => $componentLabel,
'filePath' => $component['filePath'] ?? $componentLabel,
'render_callback' => [self::class, 'acfRenderCallback'],
'category' => $gutenbergConfig['category'] ?? 'custom-blocks',
'icon' => $gutenbergConfig['icon'] ?? 'grid-view',
'keywords' => $gutenbergConfig['keywords'] ?? ['content'],
'supports' => $gutenbergConfig['supports'] ?? array(
'align' => array( 'wide', 'full' ),
'anchor' => true,
'customClassName' => true,
'color' => array('text' => true, 'background' => true),
'layout' => array('allowInheriting' => true),
'typography' => array(
'fontSize' => false,
'lineHeight' => false,
'fontWeight' => false,
),
'spacing' => array('margin' => true, 'padding' => true),
),
);
acf_register_block_type($blockConfig);
}
}
private static function getComponentGutenbergConfig($componentName): array
{
$functionName = "Flynt\\Components\\{$componentName}\\getGutenbergConfig";
if (function_exists($functionName)) {
return call_user_func($functionName);
}
return [];
}
}
BlockLinkTiles/functions.php — example block registration with field definitions including default_value:
<?php
namespace Flynt\Components\BlockLinkTiles;
use Flynt\FieldVariables;
use Flynt\Utils\GutenbergHelper;
function getACFLayout()
{
return [
'name' => 'blockLinkTiles',
'label' => __('Link Tiles', 'flynt'),
'filePath' => 'BlockLinkTiles',
'sub_fields' => [
[
'label' => __('Content', 'flynt'),
'name' => 'contentTab',
'type' => 'tab',
'placement' => 'top',
'endpoint' => 0,
],
[
'label' => __('Color', 'flynt'),
'name' => 'color',
'type' => 'select',
'choices' => FieldVariables\getThemeColorChoices(),
'default_value' => 'orange',
'wrapper' => ['width' => 100],
],
[
'label' => __('Links', 'flynt'),
'name' => 'links',
'type' => 'repeater',
'layout' => 'block',
'min' => 3,
'max' => 4,
'sub_fields' => [
[
'label' => __('Label', 'flynt'),
'name' => 'label',
'type' => 'text',
'default_value' => 'Title',
],
[
'label' => __('Link', 'flynt'),
'name' => 'link',
'type' => 'link',
'required' => 0,
],
],
],
],
];
}
function getGutenbergConfig(): array
{
return [
'icon' => 'screenoptions',
'keywords' => ['links', 'tiles', 'gradient', 'cta'],
'category' => 'custom-blocks',
];
}
add_action('acf/init', function () {
if (function_exists('acf_register_block_type')) {
$component = getACFLayout();
GutenbergHelper::registerGutenbergBlock($component);
}
});
BlockLinkTiles/index.twig — Timber template, relies on links and color being present in the context:
{% set componentOptions = {
name: 'BlockLinkTiles',
loadOn: 'visible',
class: 'has-global-padding'
} %}
{% set content %}
<div class="block-link-tiles-wrapper">
{% if links and links|length > 0 %}
{% set total = links|length %}
<div class="flex flex-col gap-sm md:flex-row">
{% for row in links %}
{% include 'Components/BlockLinkTiles/Partials/_tile.twig' with {
link: row.link,
label: row.label|default(row.link.title),
index: loop.index0,
total: total,
gradientSlugDesktop: gradientSlugDesktop|default('dark-sunset'),
gradientSlugMobile: gradientSlugMobile|default('dark-sunset-vertical')
} %}
{% endfor %}
</div>
{% endif %}
</div>
{% endset %}
{% include 'Components/_Containers/FlyntComponent.twig' with {
content: content,
componentOptions: componentOptions
} %}
Our question: what is the correct, supported way to access field default values inside render_callback on the very first render of a newly inserted block, before any data has been saved or interacted with?
Version Information:
- WordPress Version: 6.9.4
- PHP Version: 8.3
- ACF Version: ACF PRO 6.8.0.1
- Timber 2.2.0 for templating
- Browser: Brave 1.88.136
Additional context
- The block renders correctly on the website frontend
- The issue only occurs on the very first render after inserting the block in the editor — subsequent loads of an already-saved block work fine
- Ngôn ngữ chính
- PHP
- Star
- 947
- Fork
- 198
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của AdvancedCustomFields/acf
-
PHP 8.5: "Using null as an array offset" deprecation in ACF_Data::is() when rendering a block with empty data (local-meta.php is_request)Có thể đã có người làm @faisalahammad đã nhận 13 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
AdvancedCustomFields/acf#1044 ·
-
is_preview was called incorrectly notice on every early get_field() since 6.8.10 (acf_revisions::acf_validate_post_id)Có thể đã có người làm @faisalahammad đã nhận 13 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
AdvancedCustomFields/acf#1041 · 1 bình luận ·
-
True / False ACF control bug on RTL layoutsCó thể đã có người làm @elicohenator đã nhận 35 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
AdvancedCustomFields/acf#1038 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
AdvancedCustomFields/acf#1034 · 3 bình luận ·
-
Bug: class-acf-field-icon_picker.php calls wp_get_attachment_image_url() with string Dashicon slug, causing fatal TypeError on PHP 8+Có thể đã có người làm @antwonw đã nhận 47 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
AdvancedCustomFields/acf#1032 · 1 bình luận ·
Tất cả issue của AdvancedCustomFields/acf
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
backdrop-contrib/paragraphs#299 ·
-
tech-debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
landsman/adminer-desktop#206 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Explosm: Cyanide & Happiness failed with: https://explosm.net/comics/latest#comic resulted in 307Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
RSS-Bridge/rss-bridge#5154 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Sync EN] calendar: document the ValueErrors thrown for invalid calendar IDs and dates (#6005)Đang mởsync-en
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 2 ngày