Custom Fields
Re-Envisioned
Support MB Blocks Would it be possible to access a block name from within a callback/template?

  • Creator
    Topic
  • #16222
    Resolved Przemek
    Participant

    We have an access to $attributes, $is_preview, and $post_id within the callback.

    Couldn’t we have an access to the block name as well? It would be helpful when we want to handle multiple block types with a single callback function.

    Perhaps:
    $attributes['name'] or $attributes['blockname']

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #16240
    Anh Tran
    Keymaster

    Hi Przemek,

    That’s a rare case. I didn’t think about using a same callback function for different blocks.

    Anyway, new version has just been pushed and you can access to the block name via $attributes['name'].

    #20473
    badabingbreda
    Participant

    Anh,

    this isn’t working. No matter what I do, can’t get the $attributes[‘name’] because it’s not in the attributes.

    Using latest Meta Box AIO plugin.

    had to make changes to vendor\meta-box\mb-blocks\src\MetaBox.php

     public function render( $attributes = [] ) {
            $this->set_block_data( $attributes );
            $attributes[ 'name' ] = $this->id;
            ob_start();
            $post_id = get_the_ID();
            $this->render_block( $attributes, false, $post_id );
            return ob_get_clean();
        }

    to manually include name, then it worked.

    Can you update the code so this works?

    #20474
    badabingbreda
    Participant

    Since I’m unable to edit the previous comment I’ll just have to make an edit here:

    The change wasn’t enough, it was good for rendering frontend, but not in the backend. Reverted that change and simply adding it to the following part (same file) fixes it both front and backend:

     private function render_block( $attributes = [], $is_preview = false, $post_id = null ) {
    
            $attributes[ 'name' ] = $this->id;
    
            $this->enqueue_block_assests();
    
            if ( $this->render_callback ) {
                call_user_func( $this->render_callback, $attributes, $is_preview, $post_id );
                return;
            }
    
            if ( file_exists( $this->render_template ) ) {
                include $this->render_template;
            } else {
                locate_template( $this->render_template, true );
            }
        }
    #20484
    Long Nguyen
    Moderator

    Hi,

    I’m going to check it out and let you know later. Thank you.

    #20653
    Long Nguyen
    Moderator

    Hi Didou,

    Please update the new version of MB Blocks v1.2.1 or MB AIO v1.11.14 to access the block name via $attributes['name'].

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