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 Bindings: Add warning when block attribute is connected to an invalid source #64893

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useCallback, useMemo, useContext } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import isURLLike from '../components/link-control/is-url-like';
import { unlock } from '../lock-unlock';
import { Warning, useBlockProps } from '../components';
import BlockContext from '../components/block-context';

/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
Expand Down Expand Up @@ -121,9 +123,9 @@ export const withBlockBindingSupport = createHigherOrderComponent(
// used purposely here to ensure `boundAttributes` is updated whenever
// there are attribute updates.
// `source.getValues` may also call a selector via `registry.select`.
const boundAttributes = useSelect( () => {
const { boundAttributes, invalidBinding } = useSelect( () => {
if ( ! blockBindings ) {
return;
return {};
}

const attributes = {};
Expand All @@ -135,7 +137,15 @@ export const withBlockBindingSupport = createHigherOrderComponent(
) ) {
const { source: sourceName, args: sourceArgs } = binding;
const source = sources[ sourceName ];
if ( ! source || ! canBindAttribute( name, attributeName ) ) {
if ( ! source ) {
return {
invalidBinding: {
source: sourceName,
attribute: attributeName,
},
};
}
if ( ! canBindAttribute( name, attributeName ) ) {
continue;
}

Expand Down Expand Up @@ -190,7 +200,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}
}

return attributes;
return { boundAttributes: attributes };
}, [ blockBindings, name, clientId, blockContext, registry, sources ] );

const { setAttributes } = props;
Expand Down Expand Up @@ -286,6 +296,24 @@ export const withBlockBindingSupport = createHigherOrderComponent(
]
);

// Throw a warning if the block is connected to an invalid source.
if ( invalidBinding ) {
return (
<div { ...useBlockProps( { className: 'has-warning' } ) }>
<Warning>
{ sprintf(
/* translators: %1$s: block attribute, %2$s: invalid block bindings source. */
__(
'Attribute "%1$s" is connected to unrecognized "%2$s" source.'
),
invalidBinding.attribute,
invalidBinding.source
) }
</Warning>
</div>
);
}

return (
<>
<BlockEdit
Expand Down
Loading
Loading