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 a filter for the reusable block id #8912

Conversation

brucepearson
Copy link

@brucepearson brucepearson commented Aug 13, 2018

Description

Add a filter so that plugins and themes can filter the reusable block id and return a different block.

Why is this needed.

Plugins may want to display a different reusable block in some situations. Eg. WPML allows you to translate the reusable block. When rendering the page that contains the reusable block WPML can detect the current language and select the appropriate reusable block to display.

How has this been tested?

This has been tested with WPML using the follow code example.

add_filter( 'gutenberg_reusable_block_id', 'gutenberg_reusable_block_id_filter' );
function gutenberg_reusable_block_id_filter( $block_id ) {
   return icl_object_id( $block_id, 'wp_block', true );
}

Types of changes

Add a new filter

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) labels Aug 15, 2018
@gziolo
Copy link
Member

gziolo commented Aug 15, 2018

I noticed that this proposal doesn’t contain any documentation - both inline and general guidelines in the handbook how to use this hook.

@noisysocks
Copy link
Member

Cool suggestion—I think it makes sense.

I'm wondering though if we should make this more general-purpose by passing the filter the $ref and having it return the HTML that should be rendered.

I noticed that this proposal doesn’t contain any documentation - both inline and general guidelines in the handbook how to use this hook.

Yes, we'll need to document this.

@brucepearson
Copy link
Author

@noisysocks I've added inline documentation for the filter. I couldn't find where Gutenberg hooks are documented in the handbook.

@brucepearson
Copy link
Author

@gziolo We're waiting for this to improve compatibility with Gutenberg. Can this be included?

@gziolo
Copy link
Member

gziolo commented Oct 20, 2018

Sorry, I missed that! Yes, we can include it. Actually it might be very useful.

Let’s add some docs with ESNext/ES5 examples about the filter in this document:
https://github.com/WordPress/gutenberg/blob/master/docs/extensibility/extending-blocks.md.

I also agree with the suggestion from @noisysocks that it would be neat to include the html as a param in the filter for some more advanced use cases.

@gziolo
Copy link
Member

gziolo commented Oct 20, 2018

@pento @mcsf what’s the process for PHP changes now when 5.0 beta process starts?

@pento
Copy link
Member

pento commented Oct 21, 2018

PHP in the block-library will continue to be maintained in Gutenberg, as changes are usually closely tied to JS changes, too. Updates land in core by updating the version in core's package.json, running npm install, then npm webpack:dev.

@andreasciamanna
Copy link

Hi there.

Is there any change this PR will get merged and included in WP 5.0?

Copy link
Member

@pento pento left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this going in, let's just change the filter name, as it'll be going straight into Core.

*
* @param int $block_id The id of the reusable block
*/
$reusable_block_id = apply_filters( 'gutenberg_reusable_block_id', $attributes['ref'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call the filter reusable_block_id, we don't need to use the gutenberg_ prefix anymore.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@pento
Copy link
Member

pento commented Nov 4, 2018

It'd be useful if this filter also had the post ID that the reusable block is being embedding in.

@andreasciamanna
Copy link

@pento

It'd be useful if this filter also had the post ID that the reusable block is being embedding in.

Done.

I wasn't sure if I should have used global $post or, as I did here, get_post().
They should both achieve the same result, but I've used the latter more for readability purposes.

@gziolo gziolo requested a review from pento November 12, 2018 20:26
@gziolo gziolo added this to the 4.4 milestone Nov 12, 2018
@youknowriad youknowriad modified the milestones: 4.4, 4.5 Nov 15, 2018
@gziolo gziolo removed this from the 4.5 milestone Nov 16, 2018
@gziolo
Copy link
Member

gziolo commented Nov 16, 2018

It looks like a more general solution proposed in #11730 could work here, too. The only change that would need to be applied is that the other proposed filter would have to take $post_id as one of the params.

@gziolo
Copy link
Member

gziolo commented Jan 23, 2019

Closing in favor of https://core.trac.wordpress.org/ticket/45451. Thank you for opening this PR. It looks like there is ongoing discussion about enabling such capability in WordPress core.

@gziolo gziolo closed this Jan 23, 2019
@gziolo
Copy link
Member

gziolo commented Jan 23, 2019

Patch has landed in trunk:
https://core.trac.wordpress.org/changeset/44553

It should be released next month in WordPress 5.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants