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

Gutenberg compatibility for dynamic metaboxes #111

Open
6 tasks
elliotcondon opened this issue Jan 8, 2019 · 1 comment
Open
6 tasks

Gutenberg compatibility for dynamic metaboxes #111

elliotcondon opened this issue Jan 8, 2019 · 1 comment

Comments

@elliotcondon
Copy link
Contributor

Description

One of our favorite, simple and intuitive features is the ability to update metaboxes dynamically whilst editing a post. For example, metaboxes will hide or show depending on their location rules when changing the post template, format, parent and other post attributes.

To do this, we use JS to detect "changes" to the post attributes. Once a change is detected, we send an AJAX request with the current "post" information and use the returned JSON to hide, show or update the metaboxes. We also update the "Show on Screen" checkbox elements to match the visible metaboxes.

Issues

  • Existing jQuery event listeners are not compatible with Gutenberg

Questions

  • How do we listen for changes to the following post attributes?
    page_template
    page_parent
    post_format
    post_terms
  • How do we get these values on initial page load?
  • How do we get these values on change?
  • How can we hide and show metaboxes? Is CSS ok for this?
  • How can we update the "Show on Screen" check boxes to match the visible metaboxes?
@noisysocks
Copy link

Copying some answers over from WordPress/gutenberg#12692 (comment).

  • How do we listen for changes to the following post attributes?
    • page_template
    • page_parent
    • post_format
    • post_terms

If we have a React component then we can make it update when there are changes by wrapping it with withSelect(). Otherwise, we'll need to do this manually using subscribe(). I suspect that ACF will need to use the latter approach as it's built using jQuery.

let pageTemplate;
wp.data.subscribe( () => {
	const newPageTemplate = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );
	if ( pageTemplate !== newPageTemplate ) {
		console.log( 'Page template has changed!' );
		pageTemplate = newPageTemplate;
	}
} );
  • How do we get these values via the JS api?

select() and getEditedPostAttribute() is what we'll need here to access this data. The REST API reference is useful here for figuring out what post attributes are available.

const { getEditedPostAttribute } = wp.data.select( 'core/editor' );
const pageTemplate = getEditedPostAttribute( 'template' );
const pageParent = getEditedPostAttribute( 'parent' );
const postFormat = getEditedPostAttribute( 'format' );
const postTerms = getEditedPostAttribute( 'taxonomy_name' ); // e.g. 'tags', 'categories'
  • How can we hide and show metaboxes? Is CSS ok for this?

Dispatching removeEditorPanel() is likely the most resilient thing to use here. Meta box panel IDs are meta-box- followed by the ID of the meta box.

// Hides the 'postcustom' meta box
wp.data.dispatch( 'core/edit-post' ).removeEditorPanel( 'meta-box-postcustom' );
  • How can we update the "Show on Screen" check boxes to match?

By this do you mean the checkboxes that appear in the Options modal?

screen shot 2019-01-08 at 12 07 19

If so, then those can be toggled by using toggleEditorPanelEnabled().

// Toggles the 'my-box' meta box
wp.data.dispatch( 'core/edit-post' ).toggleEditorPanelEnabled( 'meta-box-my-box' );

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

No branches or pull requests

2 participants