-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block Transforms: Use of async functions in a transform #14755
Comments
Thanks for opening the issue. The
Seems like a good subject for an RFC. |
I wanted to use async too to convert a page ID to a link. As an alternative I was able to do the conversion in PHP using I look forward for the new |
Just ran into a need for async transforms myself. Perhaps we could soft-deprecate
This is a good question but I think it a shortcoming in the blocks API not the transforms API. One idea I had is to allow passing "constructor arguments" to a block. // packages/block-library/src/file/transforms.js
const blocks = createBlock( { // passing an object allows passing advanced options to createBlock
name: 'core/file',
attributes: { fileName: file.name },
innerBlocks: [],
arguments: { blobURL },
} );
// packages/block-library/src/file/edit.js
function FileEdit( { attributes, arguments } ) {
useEffect( () => {
// upload blob and call markNextChangeAsNotPersistent() & setAttributes()
}, [ arguments.blobURL ] };
} I've also seen proposals for "private" attributes which could help too.
This is a fun question. Obviously you can use the Maybe we could add a way to look up the block editor store by a block. const settings = getBlockEditorStoreForBlock( block ).getSettings(); |
Dug into this a little more. I don't think it's practical to make So I am thinking we flip this and make it so that transforms can be updated at runtime. Similar to what we already do for block styles and block variations. Then, extenders can make an API call and update the transforms when the request is fulfilled. Pseudo code: import apiFetch from '@wordpress/api-fetch';
import { addBlockTransform } from '@wordpress/blocks';
apiFetch( {
path: `/wc-blocks/v1/products/${ productId }`,
} ).then( ( product ) => {
addBlockTransform(
'from', // 'from' or 'to'
'woo/product', // identifies the transform so it can be removed later
{
type: 'block',
blocks: [ 'core/cover' ],
transform( attributes, innerBlocks ) {
// do stuff with product
return createBlock( ... );
},
}
);
} ); |
I also need to be able to make API requests inside a transform to get data from the database. |
Is your feature request related to a problem? Please describe.
I'm trying to build a transform from a WooCommerce block that displays a product using API data into the newly updated Cover block. Unfortunately, in the source block I only have the product ID attribute, and need to get the data for Cover's innerBlocks via an API request. You can see the start of this at this PR, currently using a mocked product: woocommerce/woocommerce-blocks#490
Describe the solution you'd like
It would be great if transforms could support async functions, so that I could write something like:
But that makes
switchToBlockType
more complex.Describe alternatives you've considered
My best alternative so far would be to populate some attributes for the product values I need (name, description, image src, etc), but these would not actually be editable attributes - the block would always have to pull from the product in case the source product has been updated. So that seems like an anti-pattern for attribute use.
The text was updated successfully, but these errors were encountered: