Skip to content

#52569 Create post-terms block variations for custom taxonomies in editor#52576

Closed
gaambo wants to merge 1 commit intoWordPress:trunkfrom
gaambo:52569-fix-post-terms-custom-taxonomies
Closed

#52569 Create post-terms block variations for custom taxonomies in editor#52576
gaambo wants to merge 1 commit intoWordPress:trunkfrom
gaambo:52569-fix-post-terms-custom-taxonomies

Conversation

@gaambo
Copy link
Copy Markdown
Contributor

@gaambo gaambo commented Jul 12, 2023

What?

This is a first try at fixing #52569.
I try to create the variations of the post-terms block for all custom taxonomies in the editor instead of on the server side.

Why?

See #52569 - server side function runs too early to make sure it gets all custom taxonomies by plugins, themes etc.

How?

I have to get the taxonomies from the rest api - and the results of them may come in async - therefore I have to use subscribe and unsubscribe. That's the only way I know right now how to get this kind of data (post types, taxonomies) on the "start" of the editor outside of react. If anyone has a better approach, I'm very happy to change this.
Therefore I'm using registerBlockVariation as soon as the data is available and I can't use the blocks.registerBlockType filter as in the previous code.

Some caveats:

  • Because of the REST API call, this only gets taxonomies which have "show_in_rest" enabled.
  • Maybe we can preload all the taxonomies data (like the navigation block does), but that can be a lot of data on large sites.
  • I don't know of the performance impacts of subscribe/unsubscribe but could imagine it's kind of large because
  • The _builtin property of taxonomies is not output via REST API. But by default the builtin taxonomies are registered first and therefore output first (and therefore added as a variation first).

Testing Instructions

  1. Register a custom taxonomy. (See explanation below)
  2. Create a new page/post.
  3. See that the blocks for builtin taxonomies "Tags", "Categories" are available
  4. See that a block "Courses" is available
  5. Check all three blocks are displayed with the correct name and can be switch with the variation switcher.

How to create custom taxonomy:
I've used the example code from developer.wordpress.org and put it in a simple plugin which I have mapped to load into gutenberg.

in .wp-env.json add:

	"mappings": {
		"wp-content/plugins/test-taxonomy": "../test-taxonomy"
	},

In this case i've added the sample plugin in a directory named "test-taxonomy" one level above the gutenberg directory.

TODO:

  • Maybe set one variation as the default so the "post terms" "naked" variation does not show
  • Maybe only fetch taxonomies that are releated to the currently edited post type (eg on custom post types, tags & categories would not be shown).

Screenshots or screencast

grafik

@gaambo gaambo requested a review from ajitbohra as a code owner July 12, 2023 16:24
@priethor priethor added [Block] Post Terms Affects the Post Terms Block [Type] Bug An existing feature does not function as intended and removed [Block] Post Terms Affects the Post Terms Block labels Jul 26, 2023
@gaambo
Copy link
Copy Markdown
Contributor Author

gaambo commented Nov 10, 2023

The approach in #54801 to use the registered_taxonomy seems promising, therefore probably going with this approach. If #54801 is merged, we can do the same for the post-terms block.

@gaambo gaambo closed this Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Terms Affects the Post Terms Block [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants