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

Query Block: Allow developers to extend the attributes #38163

Closed
grappler opened this issue Jan 23, 2022 · 2 comments
Closed

Query Block: Allow developers to extend the attributes #38163

grappler opened this issue Jan 23, 2022 · 2 comments
Labels
[Block] Query Loop Affects the Query Loop Block [Type] Enhancement A suggestion for improvement.

Comments

@grappler
Copy link
Member

What problem does this address?

As raised in #24934 not all the filter options are available to in the Query Loop Block. I understand that will take time. In the meantime, it would be useful to allow developers to extend the Query Block to add their own settings.

With a new PHP filter as mentioned in #36504 would allow developers to extend the output on the front end but I could not find a way to extend the query parameter of getEntityRecords( 'postType', postType, query ) to include new parameters.

Currently, I am using the following code to add a new text field to support a new include attribute, which is a comma delimited string of IDs to test.

const addIncludePosts = ( settings, name ) => {
	if ( name !== 'core/query' ) {
		return settings;
	}

	return {
		...settings,
		attributes: {
			...settings.attributes,
			query: {
				...settings.attributes.query,
				include: {
					type: 'array',
					default: [],
				},
			},
		},
	};
};

addFilter(
	'blocks.registerBlockType',
	'required/query-block/include-posts',
	addIncludePosts
);

/**
 * Override the default edit UI to include a new block inspector control for
 * assigning the anchor ID, if block supports anchor.
 *
 * @param {WPComponent} BlockEdit Original component.
 *
 * @return {WPComponent} Wrapped component.
 */
const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => {
	return ( props ) => {
		if ( props.name !== 'core/query' ) {
			return <BlockEdit { ...props } />;
		}

		const { query } = props.attributes;
		const include = query.include || [];

		return (
			<>
				<BlockEdit { ...props } />
				<InspectorControls>
					<PanelBody
						title={ __( 'Select Posts', 'ph-blocks' ) }
						initialOpen={ true }
					>
						<TextControl
							label={ __( 'Posts', 'ph-blocks' ) }
							value={ include.join() }
							onChange={ ( value ) => {
								props.setAttributes(
									{
										query: {
											...query,
											include: value.split( ',' )
										}
									}
								);
							} }
						/>
					</PanelBody>
				</InspectorControls>
			</>
		);
	};
}, 'withInspectorControl' );

addFilter(
	'editor.BlockEdit',
	'required/query-block/with-include-posts-control',
	withInspectorControl
);

What is your proposed solution?

Allow the query in PostTemplateEdit to be extended.

Related Issues

@carolinan carolinan added [Block] Query Loop Affects the Query Loop Block [Type] Enhancement A suggestion for improvement. labels Jan 24, 2022
@sc0ttkclark
Copy link

Clever solution here @grappler!

@ntsekouras
Copy link
Contributor

Thanks for the issue @msaari! I'm closing this as this has been implemented here: #44093.

You can read more about extending Query Loop in the related docs page.

Feel free to reopen, if you need something more/different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants