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

Block Hooks #1144

Closed
abhansnuk opened this issue Oct 11, 2023 · 13 comments
Closed

Block Hooks #1144

abhansnuk opened this issue Oct 11, 2023 · 13 comments
Assignees
Labels
6.4 Changes in 6.4 release [Status] Done Issue is completed
Milestone

Comments

@abhansnuk
Copy link
Collaborator

abhansnuk commented Oct 11, 2023

Marked as needing a standalone dev note on the Gutenberg repo. Two tickets were combined in Oct 2023 to be one dev note on Block Hooks area.

For info, the full list and discussion on these tickets WordPress/gutenberg#54998

@github-actions
Copy link

Heads up @WordPress/docs-issues-coordinators, we have a new issue open. Time to use 'em labels.

@abhansnuk
Copy link
Collaborator Author

abhansnuk commented Oct 11, 2023

Updating this ticket to be a combined dev note for 'Block Hooks' as this has changed in the Gutenberg tracker.

Experiment: Auto-inserting blocks on the frontend and in the editor (via REST API) #51449- Dev Note tracking: Experiment: Auto-inserting blocks on the frontend and in the editor (via REST API) #51449

WordPress/gutenberg#54293
WordPress/gutenberg#51449

@Gzielo and @ndiego working on the dev note (TBC).

@abhansnuk abhansnuk changed the title Experiment: Auto-inserting blocks on the frontend and in the editor (via REST API) #51449 Block Hooks Oct 11, 2023
@ndiego
Copy link
Member

ndiego commented Oct 11, 2023

@abhansnuk when do you need the Dev Note posted by?

@bph
Copy link

bph commented Oct 12, 2023

Hi @ndiego Ideally, we need it by Monday... but do check in if there are blockers...

@bph
Copy link

bph commented Oct 13, 2023

@shabnam611 shabnam611 added the 6.4 Changes in 6.4 release label Oct 14, 2023
@abhansnuk
Copy link
Collaborator Author

Excellent dev note @ndiego and really like your explanation and dividing this into two clear points, plus explaining why this causes some limitations.

I have done the second review for time, and

  • changed the reference to "let's us" to 'explore below" to avoid the reference to 'we' and 'us' in dev notes
  • added Developer Blog to the link to the article (good link by the way) to raise awareness that this exists
  • added an excerpt
  • added tags
  • added props line. Other collaborators or reviewers to be added to this

I wondered as this is going to be a very useful feature for eCommerce, whether there was a tag that has been used before on dev notes for flagging up notes particularly useful for eCommerce. @bph you might already know.

Thanks Nick for writing this dev note. Perhaps it is one that will give rise to some case study posts on the Dev Blog.

@ndiego
Copy link
Member

ndiego commented Oct 15, 2023

Thanks @abhansnuk! I'll add a few additional props when you are out. The code blocks still need a final review tomorrow by @gziolo.

@shabnam611 shabnam611 added this to the 6.4 milestone Oct 15, 2023
@bph
Copy link

bph commented Oct 16, 2023

I reviewed the post:

  • changed title to Introduction to Block Hooks for dynamic blocks
  • typo fixed: Blook Hooks to Block Hooks
  • added an excerpt
  • changed title "Plugins" to titled Plugins (Style guide: when referring to UI items, they need to be bolded)
  • added review props

waiting for @gziolo's review to be published

@gziolo
Copy link
Member

gziolo commented Oct 16, 2023

The example included could get some tiny changes:

function example_block_hooks( $hooked_blocks, $position, $anchor_block, $context ) {

	// Template/Template Part hooks.
	if ( $context instanceof WP_Block_Template ) {
		
		// Hooks the "Like" button block before the Post Title in the Single template.
		if ( 
			'core/post-title' === $anchor_block &&
			'before' === $position &&
			'single' === $context->slug
		) {
			$hooked_blocks[] = 'ockham/like-button';
		}

		// Hooks the Login/Logout link block after the Navigation block if the context of the template part is a header.
		if ( 
			'core/group' === $anchor_block &&
			'last_child' === $position &&
			'header' === $context->area
		) {
			$hooked_blocks[] = 'core/loginout';
		}
	}

	// Pattern hooks.
	if ( is_array( $context ) && isset( $context['slug'] ) ) {
		
		// Hooks into the Post Meta pattern in the Twenty Twenty-Four theme.
		if ( 
			'core/post-terms' === $anchor_block && 
			'after' === $position && 
			'twentytwentyfour/post-meta' === $context['slug']
		) {
			$hooked_blocks[] = 'ockham/like-button';
		}
	}

	return $hooked_blocks;
}

The list of proposed changes:

  • Stylistic: code comments end with full stop 😅
  • Stylistic: used 3rd person when starting code comments to align with coding standards 😅
  • Stylistic: there were two spaces before “after the Navigation block`.
  • It’s going to be better to check if an instance of $context is WP_Block_Template. This way, we ensure that all fields used later are defined.
  • With patterns, we don’t have the luxury of well-defined API, so we must operate cautiously. The only mandatory fields for registered patterns are title and content, so that’s why we always need to use isset or similar constructs to ensure the value is defined. In general, we should account for future changes where a different array could get passed as $context.

@bph
Copy link

bph commented Oct 16, 2023

I added updated the code examples. Thank you, @gziolo

@bph
Copy link

bph commented Oct 16, 2023

@bph bph closed this as completed Oct 16, 2023
@github-actions github-actions bot added the [Status] Done Issue is completed label Oct 16, 2023
@ndiego
Copy link
Member

ndiego commented Oct 16, 2023

Thanks everyone. I see this has been published. Team effort 💪

@abhansnuk
Copy link
Collaborator Author

Published link https://make.wordpress.org/core/2023/10/15/introducing-block-hooks-for-dynamic-blocks/
Was added to Field Guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.4 Changes in 6.4 release [Status] Done Issue is completed
Projects
Status: Done
Development

No branches or pull requests

5 participants