plone / volto

React-based frontend for the Plone Content Management System
https://demo.plone.org/
MIT License
427 stars 575 forks source link

initialValue block setting doesn't work #5971

Open wesleybl opened 1 week ago

wesleybl commented 1 week ago

Describe the bug initialValue block setting doesn't work.

To Reproduce Steps to reproduce the behavior:

  1. Create a block with the initialValue setting. For example:
    
    function initialValue({ id, value, formData, intl }) {
    return {
    ...formData.blocks[id],
    myField: 'test',
    };
    }
    const applyConfig = (config) => {
    config.blocks.blocksConfig.myBlock = {
    id: 'myBlock',
    initialValue: initialValue,
    ...
    };
    return config;
    };

2. Add the block to a page
3. The `myField` field is not filled with the value 'test'.

**Expected behavior**
The myField field should be filled with the value 'test'.

**Software (please complete the following information):**

Volto 18.0.0-alpha.27
Plone 6.0.10
plone.restapi 9.5.0

**Additional context**
The configuration does not work because the block id that is passed to the `_applyBlockInitialValue` function is the id of the previous block, and not the id of the newly created block. See:

https://github.com/plone/volto/blob/0edcd8083afee783a6463a14b18d0f712cf7c25b/packages/volto/src/helpers/Blocks/Blocks.js#L307

The id passed there should be `newBlockId` and not `id`.

Even making this fix, one thing that still happens is that when the previous one is a block of text, the setting is not applied. The [insertBlock](https://github.com/plone/volto/blob/0edcd8083afee783a6463a14b18d0f712cf7c25b/packages/volto/src/helpers/Blocks/Blocks.js#L288) method is not called in this situation. I wouldn't know where to call `_applyBlockInitialValue` in this situation. I know that the [changeBlock](https://github.com/plone/volto/blob/0edcd8083afee783a6463a14b18d0f712cf7c25b/packages/volto/src/helpers/Blocks/Blocks.js#L341) method is called in this situation. But I don't know if it would be the right place, since it is called several times.
wesleybl commented 1 week ago

@tiberiuichim any tips on how to make this work if the previous block is a text block?

wesleybl commented 1 week ago

In fact, insertBlock is not called if the previous block is an empty text block. If the text block is not empty, insertBlock is called. The difference between adding a block after any block and after a empty text block is here:

https://github.com/plone/volto/blob/24d94848dd60cb051e7a2945c26cc9c2bbb3eef8/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx#L109-L115

When the previous block is an empty text block, if returns false and then onChangeBlock is called. In other situations onSelectBlock is called.

Maybe _applyBlockInitialValue has to be called before calling onChangeBlock.

tiberiuichim commented 1 week ago

The id passed there should be newBlockId and not id.

You are correct here