Support

Account

Home Forums Gutenberg Registering an array of Gutenberg blocks

Solved

Registering an array of Gutenberg blocks

  • I have been experimenting with Gutenberg blocks recently and found that ACF 5.8 supports them, which is great!

    However, I’m not too sure on what patterns to use when it comes to register an array of blocks programmatically? I have followed a few examples successfully where a block is registered by adding the logic to functions.php or creating a separate plugin but what I mean if to have multiple block associative arrays and loop through them to register all of them (code snippet below).

    blocks.php

    /**
     * Lists all blocks to be registered.
     */
    
    $headline = array(
      'name'            => 'headlineblock',
      'title'           => __('Headline'),
      'description'     => __("A headline."),
      'render_callback' => 'block_render',
      'category'        => 'formatting',
      'icon'            => 'admin-comments',
      'keywords'        => array( 'headline', 'content' ),
    );
    
    $paragraph = array(
      'name'            => 'paragraphblock',
      'title'           => __('Paragraph'),
      'description'     => __("A paragraph."),
      'render_callback' => 'block_render',
      'category'        => 'formatting',
      'icon'            => 'editor-textcolor',
      'keywords'        => array( 'paragraph', 'content' ),
    );
    
    $blocks = [
      $headline,
      $paragraph
    ];

    render.php

    <?php
    
    /**
     *  This is the callback that renders the block.
     */
     
    function block_render( $block, $content = '', $is_preview = false ) {
    	$context = Timber::get_context();
    
    	// Store block values.
    	$context['block'] = $block;
    
    	// Store field values.
    	$context['fields'] = get_fields();
    
    	// Store $is_preview value.
    	$context['is_preview'] = $is_preview;
    
    	// Render the block.
    	Timber::render('templates/blocks/' . strtolower($block['title']) . '/index.twig', $context );
    }

    plugin index.php

    require 'render.php';
    require 'blocks.php';
    
    function block_acf_init() {
      /* Brings $blocks in to the scope of this function.
      *  It is not passed as an argument due to the function
      *  having to be called using add_action.
      */
      global $blocks;
    
      // Bail out if function doesn’t exist or no blocks available to register.
      if ( !function_exists( 'acf_register_block' ) && !$blocks ) {
          return;
      }
    
      // this loop is broken, how do we register multiple blocks in one go?
      // Register all available blocks.
      foreach ($blocks as $block) {
        acf_register_block( $block );
      }
      
    }
    
    // Initiates Advanced Custom Fields.
    add_action( 'acf/init', 'block_acf_init' );

    This case does not work for and maybe I’m missing something, but I would appreciate advice on how to handle registering a list of blocks. I’m using v5.8.0-RC2.

    Thanks in advance. 🙏

  • You should be able to call acf_register_block() (which has been renamed to acf_register_block_type() in 5.8.0 release) multiple times in a row without any issues.
    I think the issue might be that the $blocks variable does not contain the blocks data.

    try to do the end of the blocks file like so:

    
    // blocks.php
    ..
    
    $blocks = [
      $headline,
      $paragraph
    ];
    return $blocks;
    

    You can then get the value like so:

    
    // index.php
    function block_acf_init()
    {
        $blocks = require(__DIR__.'/blocks.php');
        
        foreach($blocks as $block) {
            acf_register_block($block);
        }
    }
    
  • I didn’t know this was posted successfully, thanks Jasper for your input. I have got it to work shortly after I’ve posted this.

    Also appreciate you mentioning that the function to register blocks has changed name as I am still using v5.8.0-RC2 and was not aware this was the case.

    Thank you for your help once again, I need to better understand how scope works in php.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Registering an array of Gutenberg blocks’ is closed to new replies.