AdvancedCustomFields / AdvancedCustomFields/acf

Adding a preview thumbnail to block inserter

Open
#302 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Hello,

I've tried different methods but I still can't display a block preview picture.
I've made a plugin of my block collection: https://github.com/anybodesign/ad-acf-blocks

Here is how my code works so far:

For example, registering the block "Posts":

add_action('acf/init', 'adblocks_posts_block_init');
function adblocks_posts_block_init() {

	if( function_exists('acf_register_block') ) {
		
		acf_register_block(array(
			'name'				=> 	'posts',
			'title'				=> 	__('Posts', 'adblocks'),
			'description'		        => 	__('Insert one or many posts or custom posts.', 'adblocks'),
			'category'			=> 	'ad-blocks',
			'icon'				=> 	'<svg width="100%" height="100%" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;"><g><path d="M12,0l0,2l-9.2,0c-0.442,0 -0.8,0.359 -0.8,0.8l0,18.4c0,0.442 0.358,0.8 0.8,0.8l18.4,0c0.442,0 0.8,-0.358 0.8,-0.8l0,-9.2l2,0l0,10.08c0,1.06 -0.86,1.92 -1.92,1.92l-20.16,0c-1.06,0 -1.92,-0.86 -1.92,-1.92l0,-20.16c0,-1.06 0.86,-1.92 1.92,-1.92l10.08,0Z" style="fill:#555d66;"/><path d="M18.018,9.97l-0.997,0.748c-0.997,0.997 -1.246,2.741 -0.498,3.988l-1.994,1.994l-2.742,-2.742l-3.24,3.24c-0.499,0.498 -3.988,3.24 -4.487,2.742c-0.498,-0.499 2.244,-3.988 2.742,-4.487l3.24,-3.24l-2.742,-2.742l1.994,-1.994c1.247,0.748 2.991,0.748 3.988,-0.498l0.748,-0.748c1.246,-1.246 1.246,-2.991 0.499,-4.237l1.994,-1.994l7.477,7.228l-1.994,1.994c-1.191,-0.476 -2.837,-0.497 -3.988,0.748Z" style="fill:#555d66;"/></g></svg>',
			'mode'				=> 	'auto',
			'supports'			=> 	array( 'align' => array( 'wide', 'full' ), 'mode' => false),
			'keywords'			=>	array( 'post', 'publication' ),
			'render_template' 		=>	ADBLOCKS__PLUGIN_PATH . 'blocks/block-posts/templates/block-posts-template.php',
			'enqueue_assets'		=>	function() {
										wp_enqueue_style( 'block-posts', ADBLOCKS__PLUGIN_URL . 'blocks/block-posts/css/block-posts.css' );
									},
			'example' => array(
				'attributes'		=> array(
					'mode'			=> 'preview',
					'data'			=> array(
						'content' 	=> '<img src="https://via.placeholder.com/680x720">',
					),
				)
			),
		));
	}
}
require_once( ADBLOCKS__PLUGIN_PATH . 'global-fields/columns-fields.php' );
require_once( ADBLOCKS__PLUGIN_PATH . 'blocks/block-posts/block-posts-fields.php' );
require_once( ADBLOCKS__PLUGIN_PATH . 'global-fields/background-fields.php' );

and the block template:

	$cols = get_field('columns');
	$content = get_field('posts_select');
	$h = get_field('title_level');
	$show = get_field('content_show');
	$metas = get_field('metas');
	$intro = get_field('intro');
	$intro_text = get_field('intro_text');
	
	$bgcolor = get_field('bg_color');
	$bgimg = get_field('bg_img');
	$max = get_field('center_max');
	$repeat = get_field('repeat');
	$white = get_field('white_text');
	$over = get_field('overlay');
	
	if ($bgcolor) {
		$has_bgcolor = 'background-color: '.$bgcolor.';';
	} else {
		$has_bgcolor = null;
	}
	if ($bgimg) {
		$has_bgimg = 'background-image: url('.$bgimg['url'].');';
	} else {
		$has_bgimg = null;
	} 
		
	if( !empty($block['align']) ) {
	    $align = ' align' . $block['align'];
	} else {
		$align = null;
	}						
?>

			<section class="acf-block--posts<?php if($white) { echo ' white-text'; } if( $over) { echo ' has-overlay'; } if ($repeat) { echo ' repeat'; } echo esc_attr($align); ?>"<?php if ($bgcolor || $bgimg) { echo ' style="'.$has_bgcolor.' '.$has_bgimg.'"'; } ?>>
				<div class="acf-block-container<?php if ($max) { echo ' center-max'; } ?>">
					
					<?php if ( $intro ) { ?>
					<div class="acf-block-post-intro">
						<?php echo $intro_text; ?>
					</div>
					<?php } ?>
					
					<?php if( $content ): ?>
					<div class="acf-block-post-content--<?php echo $cols; ?>">
						
				        <?php foreach( $content as $c ): ?>
				        <div class="acf-block-post-item">
					        
					        <div class="acf-block-post-figure">
						        <a href="<?php echo get_permalink( $c->ID ); ?>" title="<?php _e('Read ', 'adblocks'); echo get_the_title( $c->ID ); ?>" rel="nofollow">
					            <?php 
						            if ( has_post_thumbnail( $c->ID ) ) { 
					            		echo get_the_post_thumbnail( $c->ID, 'thumbnail-hd'); 
									} else {
										echo '<img src="' . ADBLOCKS__PLUGIN_URL .'assets/fallback.png" alt="">'; 
						        } ?>
						        </a>
						    </div>
						    
				    		<div class="acf-block-post-content">
								<<?php echo $h; ?> class="acf-block-post-title">
									<a href="<?php echo get_permalink( $c->ID ); ?>">
									<?php echo get_the_title( $c->ID ); ?>
									</a>
								</<?php echo $h; ?>>
								
								<?php if ( $metas ) { 
									$cat = get_the_category($c->ID);
									$cpt = get_post_type($c->ID)
								?>
								<div class="acf-block-post-metas">
									<span class="meta-author"><?php _e( 'Posted by&nbsp;', 'adblocks' ); echo get_the_author(); ?></span>									
									<?php if ( $cpt == 'post' ) { ?>
									<span class="meta-category"><?php _e( 'in&nbsp;', 'adblocks' ); echo '<a href="'.esc_url( get_category_link( $cat[0]->term_id) ).'">' . esc_html( $cat[0]->name) . '</a>'; ?></span>
									<?php } ?>
									
									<span class="meta-date">
										<span class="meta-date-text"><?php _e( 'on&nbsp;', 'adblocks' ); ?></span><span class="meta-date-time"><?php echo '<span class="day">'.get_the_time( ('j'), $c->ID ).'</span> '; echo '<span class="month">'.get_the_time( ('F'), $c->ID ).'</span> '; echo '<span class="year">'.get_the_time( ('Y'), $c->ID ).'</span>'; ?></span>
									</span>
								</div>
								<?php } ?>
								
								<?php 
									if ($show == 'excerpt') {
										echo '<p class="acf-block-post-excerpt">'.get_the_excerpt($c->ID).'</p>'; 
									}
									if ($show == 'content' ) {
										echo $c->post_content; 
									}	
								?>
							</div>
					        
				        </div>
				        <?php endforeach; ?>
				        
					</div>
					<?php endif; ?>	
				
				</div>
			</section>

I would like to attach a preview picture for each block but I failed at anything I've tried, following this thread, or this one :(

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ACF block registration example in the issue, especially its example attributes, and review the linked ACF discussions about inserter help panels. Trace how the registered block is rendered in the block inserter and how its template handles preview data. Done means each block displays its intended preview picture in the inserter.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.