admin管理员组

文章数量:1123945

I'm the author of the Falang multilanguage plugin for wordpress (/), trying to add a feature allowing site admins to translate their site from inside the gutenberg bloc editor

I have already make this with Elementor/Divi/Yootheme/WPbakery

The principle is to dynamically add some attributes you have to translate like input text/content/image depending of the block type

  • The content for the heading block
  • The image and alt text for the image block
  • ...

For this i use the filter getSaveElement from #gutenberg/packages/blocks/src/api/serializer.js

The serializer.js funtion has this signature

export function getSaveElement(blockTypeOrName,attributes,innerBlocks = [])

in the serializer the filter i used but the innerBlocks are not in the parameters

return applyFilters('blocks.getSaveElement',element,blockType,attributes);

my Filter getSaveElement parse the attribute for each languages and i store the render

All work fine for simple blocks but for block like core/list-item it doesn't , probably due to the innerBlocks not set correctly (not set an empty array is used)

Here is a part of the function use for the getSaveElement Filter (call saveContentOrg function)

    function saveContentOrg( element, blockType, attributes ) {

.....//logic code here

Object.values(langues)
   .forEach((langue) => {
       let locAttributes = {...attributes}
       attribs_to_change_content.forEach(attr => {

           let encodedValue = attributes[attr + langue.js_locale];
           if (encodedValue) {
               locAttributes[attr] = decode64(encodedValue)
           }
       })
       if (blockType.name != 'core/list-item'){
           var localised_content = blockType.save({attributes: locAttributes, innerBlocks: []})
       } else {
           //var localised_content = '<li>'+attributes['content']+'</li>'
       }
       if (localised_content) {
           attributes["render" + langue.js_locale] = encode64(renderToString(localised_content))
       }

   })

   // return the element wrapped in a div
   return element;
}   

My question is, is there a way to get the innerBlocks of the getSaveElement from the serailized.js in the 'blocks.getSaveElement',

Thanks a lot. Stéphane

本文标签: plugin developmentBlock Editor Filter getSaveElement and innerBlocks