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: Fix editing protected custom fields in block bindings #65658

Merged
Merged
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
7 changes: 4 additions & 3 deletions packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ function gutenberg_test_block_bindings_registration() {
'post',
'_protected_field',
array(
'type' => 'string',
'single' => true,
'default' => 'protected field value',
'type' => 'string',
'show_in_rest' => true,
'single' => true,
'default' => 'protected field value',
)
);
register_meta(
Expand Down
9 changes: 2 additions & 7 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,9 @@ export default {
return false;
}

// Check that the custom field is not protected and available in the REST API.
const fieldValue = getPostMetaFields( registry, context )?.[ args.key ]
?.value;
// Empty string or `false` could be a valid value, so we need to check if the field value is undefined.
const fieldValue = registry
.select( coreDataStore )
.getEntityRecord( 'postType', postType, context?.postId )?.meta?.[
args.key
];

if ( fieldValue === undefined ) {
return false;
}
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/specs/editor/various/block-bindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,33 @@ test.describe( 'Block bindings', () => {
previewPage.locator( '#image-alt-binding img' )
).toHaveAttribute( 'alt', 'new value' );
} );

test( 'should not be possible to edit the value of the protected custom fields', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'paragraph default content',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: { key: '_protected_field' },
},
},
},
},
} );
const paragraphBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Paragraph',
} );

await expect( paragraphBlock ).toHaveAttribute(
'contenteditable',
'false'
);
} );
} );
} );

Expand Down
Loading