Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @poena and @yochaiglik,

    I have the same problem for few days. I’m interested in more information.

    Kind regards,

    Thread Starter ostaladafab

    (@ostaladafab)

    Hi,

    Thanks threadi!

    I’ve been thinking so hard and i find a other solution :). I think the best thing to do is to use Gutenberg’s native drag and drop. I’ve developed two custom blocks. A parent block and a child block. The parent block has an innerBlock which can only receive child blocks. Here’s the simplified code.

    parent block.json

    {
        "$schema": "https://schemas.wp.org/trunk/block.json",
        "apiVersion": 2,
        "name": "domain/vegetables",
        "version": "0.1.0",
        "title": "Vegetables",
        "category": "text",
        "icon": "carrot",
        "description": "A sortable list of vegetables",
        "supports": {
            "html": false
        },
        "editorScript": "file:./index.js",
        "editorStyle": "file:./index.css",
        "style": "file:./style-index.css"
    }

    parent index.js

    import { registerBlockType } from "@wordpress/blocks";
    import metadata from "./block.json";
    import { useBlockProps, InnerBlocks } from "@wordpress/block-editor";
    
    registerBlockType(metadata, {
        edit: () => {
            const blockProps = useBlockProps();
    
            return (
                <div {...blockProps}>
                    <InnerBlocks
                        allowedBlocks={["domain/vegetable"]}
                        template={[["domain/vegetable"]]}
                    />
                </div>
            );
        },
    
        save: (props) => {
            const blockProps = useBlockProps.save();
    
            return (
                <div {...blockProps}>
                    <InnerBlocks.Content />
                </div>
            );
        },
    });

    child block.json

    {
        "$schema": "https://schemas.wp.org/trunk/block.json",
        "apiVersion": 2,
        "name": "domain/vegetable",
        "version": "0.1.0",
        "title": "Vegetable",
        "category": "text",
        "icon": "carrot",
        "parent": ["domain/vegetables"],
        "description": "A vegetable",
        "supports": {
            "html": false
        },
        "attributes": {
            "name": {
                "type": "string"
            }
        },
        "editorScript": "file:./index.js",
        "editorStyle": "file:./index.css",
        "style": "file:./style-index.css"
    }

    child index.js

    import { registerBlockType } from "@wordpress/blocks";
    import metadata from "./block.json";
    import { useBlockProps, RichText } from "@wordpress/block-editor";
    
    registerBlockType(metadata, {
        edit: (props) => {
            const blockProps = useBlockProps();
            const {
                attributes: { name },
                setAttributes,
            } = props;
    
            return (
                <div {...blockProps}>
                    <RichText
                        {...blockProps}
                        tagName="p"
                        value={name}
                        allowedFormats={[]}
                        onChange={(name) => setAttributes({ name })}
                        placeholder={"A vegetable..."}
                    />
                </div>
            );
        },
    
        save: (props) => {
            const blockProps = useBlockProps.save();
            const {
                attributes: { name },
            } = props;
    
            return (
                <div {...blockProps}>
                    <p>{name}</p>
                </div>
            );
        },
    });
    Thread Starter ostaladafab

    (@ostaladafab)

    Hi,

    I’ve just found the source of the problem. So if you encounter the same one (and if a WP core dev comes by to pick up the complaint), here’s what’s happening for me. The export actually works, but it takes an extremely long time. As there’s no message to indicate that it’s in progress, I left the page before it was finished. It’s extremely long because I have a a heavy node_modules folder from nodeJS in my theme development. Because I’ve developed custom Gutemberg blocks within my theme, using the official create-block library, which requires NodeJS.

    But even if I wait long enough for the export to finish, the resulting zipped folder is not unzippable. I get the following message: “Impossible to unzip ‘nom-du-theme’. The format is not supported”. So I’m going to delete this node_modules folder as soon as I need to export my theme and I’ll regenerate it right afterwards.

    WP core developers, it would be nice to add a hook or filter to be able to exclude files or folders from the export, like a gitignore. And for developers who also want to create an FSE theme with custom Gutemberg blocks, I can advise you to pass the blocks to a plugin. This will separate the theme from the blocks and the export should work.

    Have a nice day,
    ostaladafab

    Thread Starter ostaladafab

    (@ostaladafab)

    Hello,

    I worked around the problem, by creating an API route. Here is the simplified code of my plugin:

    class MyPlugin{
    
        function __construct(){
            add_action('rest_api_init', array($this, 'register_route'), 10, 1);
        }
    
        function register_route($wp_rest_server){
                register_rest_route(
                    'my-plugin/v1',
                    '/blocks',
                    array(
                        'methods' => 'GET',
                        'callback' => array($this, 'get_block_datas'),
                        'permission_callback' => function(){
                            return 'manage-option';
                        }
                    )
                );
            }
        }
    
    	function get_block_datas(){
    		return WP_Block_Type_Registry::get_instance()->get_all_registered();
    	}
    }
    
    new MyPlugin();

    Now, the datas I was looking for is available in JS, via the API, at this route: https://mydomainname/wp-json/my-plugin/v1/blocks.

    See you soon

    Thread Starter ostaladafab

    (@ostaladafab)

    Hello bcworkz,

    Thanks for your answer. But it is not a relative path problem. I rewrote my script for this topic. In the original script, the endpoint is full fledged. I get it from admin_url('admin-ajax.php' ), then pass it to my script with the wp_localize_script() function.

    I re-saved my permalinks configuration in the pre-production settings. Still the same problem.

    another solution ?

Viewing 5 replies - 1 through 5 (of 5 total)