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

Add support for block variations #80

Closed
bobbingwide opened this issue Sep 17, 2020 · 6 comments
Closed

Add support for block variations #80

bobbingwide opened this issue Sep 17, 2020 · 6 comments
Assignees
Labels
enhancement Gutenberg WordPress 5.0 block editor

Comments

@bobbingwide
Copy link
Owner

bobbingwide commented Sep 17, 2020

Since Feb 2020 it's been possible to register variations of blocks.

In Gutenberg 8.9.3 there are now 47 core blocks and 84 variations. 131 in total.
The following core blocks have variations:

block # variations
core/columns 5
core/embed 32
core/navigation-link 5
core/navigation 2
core/search 1
core/social-link 36

A block variation is registered with the same name as the block ( e.g. core/social-link ) to which it's a variation and has a unique name that identifies it amongst variations of that block.
Some of the fields for a variation are optional.
If undefined then the values are inherited from the parent at runtime.
For an example see the variation of core/search which has a name of default.

The variation name is not unique between blocks; both core/embed and core/social-link have variations called wordpress.

As I see it there two options for recording the information in posts:

  1. Create a new CPT called block_variation, similar to block_example, with a noderef to the parent block CPT.
  2. Create block variations as children of the parent block.

e.g. for the wordpress variation of core/social-link create the post as one of the following:

post type permalink title
block_variation wordpress-core-social-link-wordpress WordPress - core/social-link - wordpress
block wordpress-core-social-link/wordpress WordPress - core/social-link - wordpress

For the social-link blocks, the permalink using the block_variation method matches the original naming, but with a different CPT. I currently think that the hierarchical version makes the relationship clearer.

Implementation considerations

I've decided to attempt to implement the hierarchical solution.

  • Using the block CPT, the unique name for the block variation would need to be stored as _block_variation
  • Certain fields would need to be copied from the parent block.
  • At run time Gutenberg does this automagically.

Note: To make the block CPT work as a hierarchical post type in blocks.wp.a2z I needed to delete then re-add the override to the block CPT in oik-types. The override was to set the number per page to 54.
Before doing this Gutenberg was displaying a Post Attributes panel not Page Attributes.

@bobbingwide bobbingwide added enhancement Gutenberg WordPress 5.0 block editor labels Sep 17, 2020
@bobbingwide bobbingwide self-assigned this Sep 17, 2020
@bobbingwide
Copy link
Owner Author

bobbingwide commented Sep 17, 2020

I've changed the Block CPT so that it's hierarchical.
I've noticed that when I make a selection from the Parent Page: select list in the Page Attributes panel, I get a 404 in response to https://s.b/wordpress/wp-json/wp/v2/pages/1748.

This request appears to be coming from jQuery.js

Note: 1748 is the correct ID of the parent but it's not a pages, it should be block.

https://s.b/wordpress/wp-json/wp/v2/block/1748

The same thing happens for the File CPT.... which has been hierarchical for a long time now.

@bobbingwide
Copy link
Owner Author

bobbingwide commented Sep 19, 2020

New logic needs to be developed to create/update a block variation. This requires a co-requisite change in the blocklist block to add the additional query arg called variation
See bobbingwide/oik-blocks/issues/39

@bobbingwide
Copy link
Owner Author

As I see it there two options for recording the information in posts:
...
2. Create block variations as children of the parent block.

Examples of titles and permalinks for the WordPress variation of the Social link block

block permalink title
parent social-icon-core-social-link Social Icon - core/social-link
variation social-icon-core-social-link/wordpress-core-social-link WordPress - core/social-link

bobbingwide added a commit that referenced this issue Feb 25, 2021
@bobbingwide
Copy link
Owner Author

bobbingwide commented Nov 14, 2021

Well, this issue's not closed so here's a new problem.
image

It appears that the link from oik-block/blocklist to create a new block no longer works the first time it's clicked.

https://blocks.wp.a2z/wp-admin/admin-ajax.php?action=oiksc_create_or_update_block&title=Comment%20Author%20Avatar&name=core%2Fcomment-author-avatar&description=Add%20the%20avatar%20of%20this%20comment%27s%20author.&component=&keywords=&category=theme&icon=

Creating Comment Author Avatar - core/comment-author-avatar: : 4017 Created: 8779 Updating core/comment-author-avatar theme Block does not exist: core/comment-author-avatar It needs to be created first core/comment-author-avatarComment Author AvatarAdd the avatar of this comment's author.

Clicking on the link a second time results in

Updating core/comment-author-avatar theme UpdatingComment Author Avatar - core/comment-author-avatarComment Author Avatar - core/comment-author-avatar Comment Author Avatar - core/comment-author-avatar Array ( ) Array ( [0] => 648 ) core/comment-author-avatarComment Author AvatarAdd the avatar of this comment's author.[Edit]

@bobbingwide
Copy link
Owner Author

It appears that the link from oik-block/blocklist to create a new block no longer works the first time it's clicked.

The changes seem to have uncovered a couple of issues with the SQL being run.

  1. I needed to add exclude=-1 since the newly created post was not being found. bw_get_posts() automatically adds an exclude parameter for the current post ID.
  2. number_posts should have been numberposts. I don't believe this was affecting the results. But best to correct.

@bobbingwide
Copy link
Owner Author

Released in v1.41.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Gutenberg WordPress 5.0 block editor
Projects
None yet
Development

No branches or pull requests

1 participant