Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transforming wrapped content in type: 'shortcode' transforms #17758

Open
jakeparis opened this issue Oct 3, 2019 · 10 comments
Open

Support transforming wrapped content in type: 'shortcode' transforms #17758

jakeparis opened this issue Oct 3, 2019 · 10 comments
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Block Transforms Block transforms from one block to another [Feature] Convert to Blocks Internally: "raw handler" [Feature] Paste [Feature] Shortcodes Related to shortcode functionality [Type] Enhancement A suggestion for improvement.

Comments

@jakeparis
Copy link
Contributor

I am creating a block which is created when transformed from a shortcode. In other words, this:

[example att1="foo" att2="bar"]
    <p>Inner.</p>
    <p>Content.</p>
[/example]

should transform into this a myplugin/example block with inner blocks of the two p tags.

So far I've got this, which works fine to get the shortcode attributes.

(function(wp){

    wp.blocks.registerBlockType( 'myplugin/example', {
        ...

        attributes: {
            att1 {
                type: 'string',
                default: '',
            },
            att2: {
                type: 'string',
                default: '',
            },
        }, 

        transforms: {
            from: [
                {
                    type: 'shortcode',
                    tag: 'example',
                    attributes: {
                        att1: {
                            type: 'string',
                            shortcode: attributes => {
                                return attributes.named.att1 || '';
                            }
                        },
                        att2: {
                            type: 'string',
                            shortcode: attributes => {
                                return attributes.named.att2 || '';
                            },
                        },
                    },
                },
            ],
        },
        ...

I can get the content by looking at the second parameter in one of those attributes shortcode functions:

attributes: {
    att1: {
        shortcode: (attributes, data) => {
             // data.shortcode.content is the shortcode's inner content
        }
    }
}

But I don't know how to insert that content as innerBlocks in the output block. The block registration "transforms" attribute documentation is not helpful in this area.

I've tried using the transform function along with type: "shortcode", but they don't seem
to work together.

@swissspidy swissspidy added [Type] Help Request Help with setup, implementation, or "How do I?" questions. [Feature] Block API API that allows to express the block paradigm. [Feature] Shortcodes Related to shortcode functionality labels Oct 4, 2019
@simonhammes
Copy link
Contributor

As far as I know this is currently not possible. The transform mechanism for shortcodes does not allow returning a block object. However, this is something I would like to see in the future, similar to how block -> block transforms work.

In the meantime, one possible hack is to create and insert a new block with the corresponding inner blocks inside of the arrow function. Unfortunately you then have to manually delete the second, empty block.

@jakeparis
Copy link
Contributor Author

The way I got around this was to add a block -> block transform (from core/shortcode to my custom one) which manually parses out the shortcode attributes and content. But this is still a two-step transform, as first we have to transform to the shortcode block, then transform to my custom block.

It feels like a no-brainer that the "shortcode" type should be able to automatically use the inner content of the shortcode as innerBlocks with no configuration. I wouldn't mind working on that myself and perhaps submitting a PR, but I can't find where that lives in the code.

Can we add a label for "Feature Request" or "Enhancement Request" ?

@kadamwhite kadamwhite added [Feature] Block Transforms Block transforms from one block to another [Feature] Paste [Type] Enhancement A suggestion for improvement. and removed [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Dec 4, 2019
@greatislander
Copy link
Contributor

Related to #6020.

@kadamwhite
Copy link
Contributor

We ran into this as well. The best interim approach we found was to convert the shortcode's inner content string into a temporary attribute on the block, and then whenever the block's Edit method rendered and that attribute was present, we unset the attribute and used pasteHandler to convert that content to blocks and insert as children of the current block. Clunky; it'd definitely be best to support this conversion natively!

@simonhammes
Copy link
Contributor

@kadamwhite Does this work with nested shortcodes?

@kadamwhite kadamwhite changed the title In a block transform (type: shortcode), how do I use shortcode "inner content" as block's innerBlocks? Support transforming wrapped content in type: 'shortcode' transforms Mar 10, 2020
@kadamwhite
Copy link
Contributor

Renamed the issue for hopefully greater clarity. cc @ellatrix to make sure this isn't an overstep :)

@ocean90
Copy link
Member

ocean90 commented May 11, 2020

@kadamwhite Can you point me to an example for the workaround with the pasteHandler() in combination with InnerBlocks? Would like to extend this in the callout block on w.org.

@kadamwhite
Copy link
Contributor

@ocean90 i’m overdue to write up that trick, so keep pressing me on that point, but I don’t want the presence of a workaround to distract from fixing the underlying issue

@efc
Copy link

efc commented Sep 24, 2022

Any progress on this? It is really unfortunate that the shortcode transform can't handle such a common shortcode use case!

@kadamwhite, the pasteHandler() workaround sounds very helpful. If you have written that up or have an example, I'd love a pointer.

@wpexplorer
Copy link

Any progress on this?

@ellatrix ellatrix added the [Feature] Convert to Blocks Internally: "raw handler" label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Block Transforms Block transforms from one block to another [Feature] Convert to Blocks Internally: "raw handler" [Feature] Paste [Feature] Shortcodes Related to shortcode functionality [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

9 participants