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

Post Content block: Allow Block Hooks child insertion #56972

Closed
wants to merge 2 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Dec 12, 2023

Untested; this code probably doesn't work yet.

What?

Try allowing first_child and last_child insertion into the Post Content block.

This is experimental; there's a pretty good chance that we'll never merge this code due to its limitations (see "Notes" section below).

Why?

It was brought up that this would be beneficial e.g. for inserting a Like Button block as the Post Content block's last_child (rather than after) for consistency with the Classic Theme way of doing this (using the the_content filter).

How?

By manually applying the hooked_block_types filter inside the Post Content block.

Testing Instructions

Basically, follow Block Hooks instructions to insert a block as core/post-content's last_child.

Notes

Note that due to the fact that we're doing this manually, this approach has a number of significant shortcomings, among them:

  • Blocks hooked to these locations will not show up in the editor -- violating a core tenet of Block Hooks.
  • Changes to anchor block by get_hooked_block_markup() aren't respected.
  • No $context available.

Alternatives considered

We could run our Block Hooks logic on the $content returned from get_the_content() (in the Post Content block's render method), or even generically on all post content (in any given context; e.g. when running do_blocks).

However, that would present a pretty big departure from how it's worked up to now (which was pretty much limited to templates, parts, and patterns). It's something to consider for WP 6.5 (since it might be possible thanks to the new mechanism we're using), but it'll require careful deliberation.

@ockham ockham self-assigned this Dec 12, 2023
Copy link

github-actions bot commented Dec 12, 2023

Flaky tests detected in 74439b5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7198507644
📝 Reported issues:

@ockham
Copy link
Contributor Author

ockham commented Jan 9, 2024

I'll close this, as I'm fairly convinced that it isn't the right way forward -- especially when it comes to inserting a Like Button block below a Post Content block. Some of the downsides listed in the notes in the PR description are dealbreakers; it's awkward to try and "emulate" the Block Hooks mechanism like this, and doesn't scale to other blocks. Finally, on a conceptual level, it's better to insert a Like Button block after a Post Content block, rather than as its last child.

This means that to solve the original problem, I'll focus on finding a solution for this issue.

It's still possible that we'll need to apply a solution similar to what's been explored in this PR to some other blocks (e.g. Navigation) to make child insertion work.

@ockham ockham closed this Jan 9, 2024
@ockham ockham deleted the try/supporting-post-content-child-block-hooks branch January 9, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant