-
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
Navigation: allow plugins to register icons for custom post type and taxonomy variants #31226
Comments
@Aljullu I agree we should enable the ability to register custom icons for the You will also notice that once you insert one of the block variations for a custom post type the name of the block in the block sidebar still reads I'm currently working on #31004 which standardizes the serialization of blocks to nav menu items (and vica versa) so that existing menus will translate perfectly over to the new Nav editor screen. I'd love some more 👀 on that if possible. |
Yes, I hope we'll have this in the future. The current variations API only allows icons to be set with a JS function. We need to explore if we can set icons as a react component function OR svg string (and still have this work for React Native). Once an icon is serializable, it'll be possible then to update the server API. The workaround for right now is adding a block filter, which even the core/navigation-link needs to do: addFilter(
'blocks.registerBlockType',
'core/navigation-link',
( settings, name ) => {
if ( settings.variations ) {
const variations = settings.variations.map( ( variation ) => {
return {
...variation,
...( ! variation.icon && {
icon: getIcon( variation.name ), //add your custom icon here
} ),
};
} );
return {
...settings,
variations,
};
}
return settings;
); |
To fix that, when registering the custom post type, add two new labels |
Thanks for clarifying those concepts to me, I'm still new to all the Gutenberg nomenclature. 🙂
Will do. Adding this to my todo list for this week. 👍 |
That worked great, thanks for pointing me in the right direction! I needed to tweak the code a bit because when calling it from a plugin, function replaceIcon( variation ) {
switch ( variation.name ) {
case 'my-variation':
return {
...variation,
icon: <MyIcon />,
};
default:
return variation;
}
}
addFilter( 'blocks.registerBlockType', 'my-plugin/navigation-link-icons', ( settings ) => {
if ( settings.variations ) {
const variations = settings.variations.map( replaceIcon );
return {
...settings,
variations,
};
}
return settings;
} ); |
Hey @getdave - just triaging older issues - and wanted to see if we had inadvertently solved this one in the last 2 years? Or if it still needs working on. |
I don't think this has been resolved no. |
Navigation inner blocks are automatically available for Custom Post Types and taxonomies. That was introduced in #29095 and works great, making plugin authors life much easier. However, it doesn't allow setting a custom icon for those inner blocks.
In the screenshot below, you can see the Product Link, Product Category Link and Product Tag Link blocks added by WooCommerce using the generic icon:
I anticipate this will be an issue for sites with several plugins installed.
@gwwar do you have any thoughts about whether an interface could be added so plugins can register custom icons for the Navigation inner blocks which are generated automatically?
Step-by-step reproduction instructions
The testing steps from #29095 showcase the issue.
Expected behaviour
As a plugin author, I would like to be able to set custom icons for the custom post types and taxonomies registered in my plugin.
Actual behaviour
AFAIK, currently there is no API do register icons for CPT and taxonomy link blocks.
The text was updated successfully, but these errors were encountered: