Update/editor template validation #38770
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is my attempt at fixing #11681
This template validation issue has been bothering me for a while now so I thought I would have a stab at it.
Description
The issue, as far as I can tell, is that only a basic block validation is performed by
doBlocksMatchTemplate
where the innerblock count and name of each block in the tree are checked recursively against the template. This happens early in the editor lifecycle, before the blocks have been parsed, which means there is no way of knowing what properties have been assigned to each innerblocks component.The problem arises when an innerblocks component somewhere in the tree has its
templateLock
property set to false. This allows a user to add any blocks permitted by theallowedBlocks
property. This results in the template tree becoming out of sync with the template registered to a given post typeMy solution to this problem is to wait until the block tree has been parsed and rendered by way of
useLayoutEffect
inpackages/block-editor/src/components/provider/index.js
where a call is made tovalidateBlocksToTemplate
rather than its original location inpackages/block-editor/src/store/actions.js
.This is probably no the best solution, as I'm sure there is a store action one could subscribe to rather than relying on
useLayoutEffect
; however, I was unable to find any suitable action.This change then permits the block list of each innerblocks component to be checked against the blockeditor store using
select.getBlockListSettings
Unfortunately, I was unable to import the blockEditor store into
packages/blocks/src/api/templates.js
where thedoBlocksMatchTemplate
function is located to make use ofgetBlockListSettings
. Doing so resulted in a php out of memory error in thewp_scripts
class. I can only assume I must have created some sort of circular dependency which caused the editor to enqueue an infinite number of scripts. to work around this, I passed thegetBlockListSettings
function through todoBlocksMatchTemplate
fromvalidateBlocksToTemplate
inpackages/block-editor/src/store/actions.js
I'm sure this is not the best solution and should be reviewedThe above efforts allowed me to check the
templateLock
property of any innerblocks component encountered in the block tree and skip traversal of that subtree if it isfalse
Testing Instructions
Register a block whose innerblocks have no specified template and have its
templateLock
explicitly set tofalse
:Register a template for a post type:
Edit a page and add some blocks to the inner blocks component
Save the page and reload.
A successful test should result in no error message being displayed
Checklist:
*.native.js
files for terms that need renaming or removal).I'm not at all familiar with React Native