-4

I've created a custom Gutenberg block and would like to know how to add more within the same module. Some suggested to add JS in MY_CustomModule.libraries.yml, but it does not work.

My custom H2 heading block in index.es6.js. This simple block is working fine.

const { blocks, data, element, components, editor } = wp;
const { registerBlockType } = blocks;
const { dispatch, select } = data;
const { Fragment } = element;
const { PanelBody, BaseControl, Icon, RangeControl, IconButton, Toolbar, SelectControl } = components;
const { InnerBlocks, RichText, InspectorControls, PanelColorSettings, MediaUpload, BlockControls } = editor;
const __ = Drupal.t;

const settings = {
  title: __('Section Heading'),
  description: __('H2 Heading'),
  icon: 'heading',
  attributes: {
    title: {
      type: 'array',
      source: 'children',
      selector: 'h2',
    },
  },

  edit({ className, attributes, setAttributes, isSelected }) {
    const { title } = attributes;
    return (
      <Fragment>

            <RichText
              identifier="title"
              tagName="h2"
              value={ title }
              onChange={nextTitle => {
                setAttributes({
                  title: nextTitle,
                });
              }}
              placeholder={ __( 'Enter text...', 'custom-block' ) }
              keepPlaceholderOnFocus={true}
            />
        <InspectorControls>
          <PanelBody title={ __('Block Settings') }>
            { attributes.content }
          </PanelBody>
        </InspectorControls>
      </Fragment>
    );
  },
  save({ className, attributes }) {
    const { title } = attributes;
    return (
        <h2>{ title }</h2>
    );
  }
};
const category = {
  slug: 'myblocks',
  title: __('My Blocks'),
};

const currentCategories = select('core/blocks').getCategories().filter(item => item.slug !== category.slug);
dispatch('core/blocks').setCategories([ category, ...currentCategories ]);

registerBlockType(`${category.slug}/docs-blocks`, { category: category.slug, ...settings });

1 Answer 1

1

You simply need to reproduce the structure of your block inside the module :

- Module root
-- blocks
--- first_block
---- js/index.js
---- js/index.es6.js
--- second_block 
---- js/index.js
---- js/index.es6.js
- module.gutenberg.yml 
# module.gutenberg.yml file content  = Libraries to be injected on editor (node edit)
libraries-edit:
  - module/first_block-edit
  - module/second_block-edit
- module.librairies.yml
# module.librairies.yml content = front libs :
first_block-edit:
  version: VERSION
  js:
    blocks/first_block/js/index.js: {}
  dependencies:
    - gutenberg/edit-node

second_block-edit:
  version: VERSION
  js:
    blocks/second_block/js/index.js: {}
  dependencies:
    - gutenberg/edit-node

You could add as many gutenberg block as you need as long as your register your block with a different category/slug in the index.es6.js

registerBlockType( `${category.slug}/${block.slug}`, { category: category.slug, ...settings } );

4
  • What about the "main:" on package.json? How am I add the 2nd js? Can that be "main": ["blocks/first_block/js/index.js", "blocks/second_block/js/index.js"], ?
    – CocoSkin
    Commented Oct 29, 2020 at 21:06
  • It works only if I assign with a different category in index.js. const category = { slug: 'myblocks', title: __('myblocks'), };
    – CocoSkin
    Commented Oct 29, 2020 at 21:24
  • I found the category solution from here drupal.org/project/gutenberg/issues/3058354 . Many thanks by the way.
    – CocoSkin
    Commented Oct 29, 2020 at 21:35
  • I use the same category but a different slug. The category allows to organize the presentation gutenberg blocks - I'll edit the answer.
    – Simon Vart
    Commented Oct 30, 2020 at 9:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.