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

[Create Block] Add the capability of defining a function from a template to modify values passed to create the block #55421

Closed
ryanwelcher opened this issue Oct 17, 2023 · 3 comments · Fixed by #55423
Assignees
Labels
Developer Experience Ideas about improving block and theme developer experience [Package] Create Block /packages/create-block [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement. [Type] Feature New feature to highlight in changelogs.

Comments

@ryanwelcher
Copy link
Contributor

What problem does this address?

Currently it's not possible to modify any of the values passed to create-block either from the CLI or a custom template. This means that any new functionality must be added via a flag and that there is no way to customize a large portion of these values in a template. For example, textdomain is always the slug value that is passed and is not changeable.

What is your proposed solution?

I would like to introduce a transformer property that can be defined in an external template. This property would expect a function that receives all of the values that will be used to generate the files. This feature would provide complete control over the values being output and allow for conditional values based on others.

Example

In this example, we can generate a unqiue id that is prepended to the passed slug but then also usable for other properties.

transformer: ( view ) => {
    const hex = getRandomHexCode();
	return {
		...view,
		slug: `${ view.slug }-${ hex }`,
		customBlockJSON: {
			...view.customBlockJSON,
			keywords: [ ...view.customBlockJSON.keywords, hex ],
		},
	};
},
@ryanwelcher ryanwelcher added [Type] Feature New feature to highlight in changelogs. [Package] Create Block /packages/create-block Developer Experience Ideas about improving block and theme developer experience labels Oct 17, 2023
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Oct 17, 2023
@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Oct 17, 2023
@gziolo
Copy link
Member

gziolo commented Oct 18, 2023

That's a pretty powerful idea, I will leave my feedback on the proposed implementation with #55423.

@gziolo
Copy link
Member

gziolo commented Oct 18, 2023

For example, textdomain is always the slug value that is passed and is not changeable.

This one should be tackled separately as I saw an issue raised somewhere that it's impossible to set the textdomain when scaffolding a block without a plugin. There usually should be only one textdomain set for the plugin.

@ryanwelcher
Copy link
Contributor Author

With the changes I just pushed, it is possible to manage the textdomain via the transformer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Experience Ideas about improving block and theme developer experience [Package] Create Block /packages/create-block [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement. [Type] Feature New feature to highlight in changelogs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants