6. Extend volto blocks – Volto Customization for JavaScript Beginners

Extend volto blocks

6. Extend volto blocks#

There are various ways of extending Volto blocks. Component shadowing (see last chapter) is a very basic to customize components in volto. But it comes with its own problems like keeping the shadowed component up to date with latest fixes and features of newer Volto versions. Instead of shadowing components we can:

  • Change the block-config

  • Extend blocks by adding new block-variations

  • Write add schemaEnhancer to modify blocks schema

Let us first change the View of the teaser block which we already have in volto core by changing the block-configuration. In our addon volto-teaser-tutorial we will step by step extend each component that we have in volto core.

The most simple customization is the View of the Teaser. The volto core teaser block configration (in omelette/src/config/Blocks.jsx) looks like:

  teaser: {
    id: 'teaser',
    title: 'Teaser',
    icon: imagesSVG,
    group: 'common',
    view: TeaserViewBlock,
    edit: TeaserEditBlock,
    restricted: false,
    mostUsed: true,
    sidebarTab: 1,
    blockSchema: TeaserSchema,
    dataAdapter: TeaserBlockDataAdapter,
    variations: [
      {
        id: 'default',
        isDefault: true,
        title: 'Default',
        template: TeaserBlockDefaultBody,
      },
    ],
  },

Every block in Volto has Edit and View components. You can customize these individually by either shadowing or directly in the confuguration (index.js of your addon) like this:

import MyTeaserView from "volto-teaser-tutorial/components/Blocks/Teaser/View";

const applyConfig = (config) => {
  config.blocks.blocksConfig.teaser.view = MyTeaserView;
  return config;
};

export default applyConfig;

Of course we need to add our custom MyTeaserView component in our addon. From the root of the project that is src/addon/volto-teaser-tutorial/src/components/Blocks/Teaser/View.jsx:

import React from "react";
import TeaserBody from "@plone/volto/components/manage/Blocks/Teaser/Body";
import { withBlockExtensions } from "@plone/volto/helpers";

const TeaserView = (props) => {
  return <TeaserBody {...props} extraProps={{ foo: "bar" }} />;
};

export default withBlockExtensions(TeaserView);

Here, the View component renders a TeaserBody which will be a result of an active variation, we will come to that in later chapters.