Как исправить ошибку «Ошибка загрузки блока: неверные параметры: атрибуты» в WordPress - PullRequest
1 голос
/ 07 июня 2019

Я пытаюсь расширить существующий блок, который зарегистрирован в плагине Tasty Recipes. Я добавил элемент управления на боковую панель, чтобы обрабатывать выбор данного типа диеты, однако, когда выбран вариант, отображается следующая ошибка:

Ошибка загрузки блока: неверные параметры: атрибуты

В настоящее время работает на WordPress 5.2.1, и я пытаюсь расширить плагин Tasty Recipes

extendTastyRecipes.js

/**
 * Add custom attribute to store diet type
 */

var addCustomAttributes = function( settings, name ) {

    if ( name !== 'wp-tasty/tasty-recipe' ) {
        return settings;
    }

    settings.attributes = Object.assign( settings.attributes, {
        dietType: {
            type: 'string',
            // ? Setting default here causes break
        },
    } );

    return settings;
}

wp.hooks.addFilter( 'blocks.registerBlockType', 'wp-tasty/tasty-recipe', addCustomAttributes );


/**
 * Create HOC to add diet type control to inspector controls of block.
 */
var el = wp.element.createElement;
var withInspectorControls = wp.compose.createHigherOrderComponent( function( BlockEdit ) {
    return function ( props ) {

        function onChangeDietType (newDietType) {
            props.setAttributes( { dietType: newDietType } );
        }

        return el(
            wp.element.Fragment,
            {},
            el(
                BlockEdit,
                props
            ),
            el(
                wp.editor.InspectorControls,
                {},
                el(
                    wp.components.PanelBody,
                    {},
                    el (
                        wp.components.SelectControl,
                        {
                            label: 'Diet Type',
                            onChange: onChangeDietType,
                            options: [
                                { label: 'Paleo', value: 'paleo' },
                                { label: 'Vegan', value: 'vegan' },
                                { label: 'Vegetarian', value: 'vegetarian' },
                            ],
                        },
                    )
                )
            )
        );
    };
    }, 'withInspectorControls' );
    wp.hooks.addFilter( 'editor.BlockEdit', 'wp-tasty/tasty-recipe', withInspectorControls );


    /**
     * Not sure this is even necessary as the plugin I'm extending handles rendering on server
     *  -- extra --
     * @param {Object} extraProps 
     * @param {Object} blockType 
     * @param {Object} attributes 
     */
    function addSaveProps( element ) {
        return element; // This returns null
    }
    wp.hooks.addFilter( 'blocks.getSaveElement', 'wp-tasty/tasty-recipe', addSaveProps );

functions.php

function extendTastyRecipes() {

    $blockPath = get_stylesheet_directory_uri() . '/assets/scripts/modules/extendTasyRecipes.js';

    wp_enqueue_script(
        'extend-tasty-recipes-js',
        $blockPath,
        [ 'wp-i18n', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-edit-post', 'wp-api' ]
    );
}
add_action('init', 'extendTastyRecipes');

Надеюсь, я упускаю что-то простое ... Не совсем знаком с Гутенбергом, так что я могу просто неправильно понять документы.

...