script tag: error in conditional-logic.js

Support MB Blocks script tag: error in conditional-logic.js

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #37950
    Aaron KesslerAaron Kessler
    Participant

    As soon as there is a script tag inside my block, I get an error which is preventing the gutenberg-editor to load:

    conditional-logic.js?ver=1660849891:555 Uncaught TypeError: Cannot read properties of undefined (reading 'subscribe')
        at watch (conditional-logic.js?ver=1660849891:555:12)
        at init (conditional-logic.js?ver=1660849891:576:3)

    Example-Block:

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'       => __( 'slider-test', 'your-text-domain' ),
            'id'          => 'slider-test',
            'category'    => 'text',
            'supports'    => [
                'align' => [''],
            ],
            'render_code' => '<script>
    </script>',
            'type'        => 'block',
            'context'     => 'content',
            'fields'      => [
                [
                    'name' => __( 'Text', 'your-text-domain' ),
                    'id'   => $prefix . 'text_mmqz4db9cg',
                    'type' => 'text',
                ],
                [
                    'name'     => __( 'Image Advanced', 'your-text-domain' ),
                    'id'       => $prefix . 'image_advanced_sesv7f22zp',
                    'type'     => 'image_advanced',
                    'required' => true,
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    #37966
    Long NguyenLong Nguyen
    Moderator

    Hi Aaron,

    If you use the PHP code to register the custom block, please use the setting render_callback or render_template with PHP code. Read more on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback

    If you use the builder, please remove the script tag in the code area and re-check this issue.

    #37967
    Aaron KesslerAaron Kessler
    Participant

    Sure, if I remove the script tags, the error is gone. I switched to reading data-attributes instead of inlineing JS to avoid this altogether. However, there is no difference in using render_callback or render_template, it's still not possible to use a script tag.

    My example callback is

    
    function test_callback($attributes, $is_preview = false, $post_id = null)
    {
        ?>
    <script></script>
    hello from render callback
    <?php
    }
    
    #37980
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you want to use the script code on the frontend, please check this sample code

    function test_callback( $attributes, $is_preview = false, $post_id = null ) {
        echo mb_get_block_field( 'section_title' );
        ?>
        <script type="text/javascript">
            console.log('hello from render callback');
            alert('Hello MB Blocks');
        </script>
        <?php
    }

    If you want to preview the script code on the backend, please follow this documentation https://docs.metabox.io/extensions/mb-blocks/#enqueue_script

    #37981
    Aaron KesslerAaron Kessler
    Participant

    It is an error in conjunction with other blocks. I recreated the hole page block by block to track down the error, but I am not able to reproduce it.

    #38005
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Can you please let me know which standard blocks of WordPress that cause the issue with conditional logic in the custom block? Please try to deactivate all plugins except Meta Box, MB extensions, switch to the WP standard theme (Twenty TwentyTwo) and recheck this issue.

    #38011
    Aaron KesslerAaron Kessler
    Participant

    It was happening with my own custom blocks, I tied them to my custom theme, so switching to Twenty TwentyTwo is pointless. No standard blocks involved. I did not find any errors in my own blocks and since it is not happing on a new page with the same block arrangement I am totally okay with not debugging it further. The first log error is Uncaught SyntaxError: Unexpected token '<' (at post.php?post=2&action=edit:125:1) which I don't know what to do with, followed by a bunch of "unable to register block type errors" for all my own blocks. And the mb-block related error I posted above.

    #38030
    Long NguyenLong Nguyen
    Moderator

    Hi,

    After using the callback function instead of setting render_code, you can delete all blocks on the page and re-add them. Or just create a new page to avoid this issue.

    Let me know if you have any questions.

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