简体   繁体   English

ACF 古腾堡块中的预览图像

[英]Preview image in ACF gutenberg block

Is it possible to add an image to the gutenber block preview when using ACF to register the block?使用 ACF 注册块时,是否可以将图像添加到 Gutenber 块预览中?

在此处输入图像描述

Here's the code to register the block:这是注册块的代码:

acf_register_block(array(
    'name'              => 'bk-raisons',
    'title'             => __('Les raisons', 'diezel'),
    'description'       => __('Les raisons', 'diezel'),
    'render_callback'   => 'my_acf_block_render_callback',
    'category'          => 'spira-custom',
    'icon'              => 'align-wide',
    'keywords'          => array('bk-raisons'),
));

The preview appears when hovering the block.悬停块时会出现预览。

Thank you !谢谢 !

I finally found a solution.我终于找到了解决方案。

I don't know if you use Twig (Timber) or not.我不知道您是否使用 Twig(木材)。

If not check this: https://stackoverflow.com/a/67846162/6696150如果不检查这个: https://stackoverflow.com/a/67846162/6696150

For my part with Timber就我而言,木材

When you declared your block add example attributes:当您声明您的块时,添加示例属性:

$img_quadruple = array(
    'name'              => 'img-quadruple',
    'title'             => __('Quatre images'),
    'title_for_render'  => 'img-quadruple',
    'description'       => __(''),
    'render_callback'   => 'ccn_acf_block_render_callback',
    'category'          => 'imgs',
    'icon'              => '',
    'mode'              => 'edit',
    'keywords'          => array( 'quatre images' ),
    'example'           => array(
        'attributes'        => array(
            'mode' => 'preview',
            'data' => array(
                'preview_image_help' => get_template_directory_uri().'/assets/img/preview/preview_img_quadruple.jpg',
            ),
        )
    )
);

And when your declared your callback:当你声明你的回调时:

function ccn_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
    $context = Timber::context();

    // Store block values.
    $context['block'] = $block;

    // Store field values.
    $context['fields'] = get_fields();

    // back-end previews
    if ( $is_preview && ! empty( $block['data'] ) ) {
        echo '<img src="'. $block['data']['preview_image_help'] .'" style="width:100%; height:auto;">';
        return;
    } elseif ( $is_preview ) {
        echo 'Other condition';
        return;
    }

    // Store $is_preview value.
    $context['is_preview'] = $is_preview;

    // Render the block.
    Timber::render('gutenberg/gut_' . strtolower($block['title_for_render']) . '.twig', $context );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM