Custom Fields
Re-Envisioned
Support Meta Box Builder PHP Block generated from Builder not rendering in editor or front end.

  • Creator
    Topic
  • #31747
    Resolved Matt V
    Participant

    I'm using Builder to create a small library of Gutenberg blocks, mostly because I'd like to use twig. I intend to use these blocks with multiple sites, so I'm hoping to create a block library plugin.

    I've created a simple block in Builder with a single text field and a very simple twig rendering. It appears as expected in the block selector, the field shows up as expected in the sidebar, and the block renders as expected in the editor and on the front end once the field has content.

    Then I generate PHP for the block in Builder and add it to my plugin. I expect that the block will act the same way: show up in the selector, show the field in the sidebar, and render in the editor and the frontend once the field has content.

    That's not what happens. It does appear in the block selector. It does show the field in the sidebar. So it registers properly. But when I enter content, the block does not render in the editor. And it does not render on the front end. There is no code at all for the block in the inspector.

    I'm puzzled as to why this is.

    Here is the PHP block code:

    <?php
    add_filter( 'rwmb_meta_boxes', 'register_test_block_php' );
    
    function register_test_block_php( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'       => __( 'Test Block - PHP', 'your-text-domain' ),
            'id'          => 'test-block-php-14',
            'icon'        => 'flag',
            'category'    => 'text',
            'keywords'    => ['block test'],
            'supports'    => [
                'align' => [''],
            ],
            'render_code' => '<div class="test_block">Field text: {{ php_block_field_text }}</div>',
            'type'        => 'block',
            'context'     => 'side',
            'fields'      => [
                [
                    'name' => __( 'Field Text', 'your-text-domain' ),
                    'id'   => $prefix . 'php_block_field_text',
                    'type' => 'text',
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Just to be clear, I changed the ID of the block and of the field so there wouldn't be a conflict with the Builder-generated block.

    I'm sure there's something simple I'm missing, but I'd love your help in figuring out what.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #31755
    Long Nguyen
    Moderator

    Hi,

    The feature Get PHP Code of the builder does not support converting Twig code to PHP code. So you have to change the parameter render_code to render_callback or render_template and change the code to PHP code to render elements on the frontend. Please read more on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback

    #31758
    Matt V
    Participant

    Hi Long. Thanks for your response. I guess I got my hopes up that I'd be able to build my MB Twig blocks into a plugin. I'll think about whether to go the PHP route or to just build in Builder and export/import to new sites with JSON.

    I know this is a separate question, but as long as I have you here... If I build several blocks in Builder, is there a way to batch export/import them all into a new site? Or would I need to do so one at a time?

    #31771
    Long Nguyen
    Moderator

    Hi,

    You can export field groups then import them to other sites, please get more details on this documentation https://docs.metabox.io/extensions/meta-box-builder/#export--import

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.