Problem/Motivation
When using a custom content block type which has a media reference field using the media library browser, I get the following error:
"Non-reusable blocks must set an access dependency for access control"
However, this is only under some circumstances. (see steps to reproduce below)
After some digging around, I attempted to add a SetContentBlockDependency.php event Subscriber similar to how layout_builder does with SetInlineBlockDependency.php. Where on BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY
I set a dependency to the node entity on the content block using the newly added content block usage info that was added in
π
Duplicating Drupal content blocks in editor should create a new block entity
Fixed
This gets me part way there, and resolves the issue when editing already saved nodes. However, I am running into trouble with new nodes that haven't been saved, and therefore there is no usage data yet. (there is no node id to track usage)
In this case I do not see a way of setting a dependency entity to the content block, since none exists yet.
The reason this works for layout builder, is that there, the layout entity is being used as the dependency (not a node entity) and that does exist by the time you are adding blocks. This is not the case with gutenberg.
Steps to reproduce
- Create a content block that has a media field and uses the media library widget for selecting media.
- Enable this block type in gutenberg
- Create a new gutenberg node
- Add the content block with the media field.
- Add media to the blocks field.
- Click the update block button (this saves the non-reusable content block)
- edit the content block again, this time remove a media item, and try adding a new media item.
- The error is triggered. (in the js console) and you are unable to change the media entity.
However, if you skip step 6 (updating the block, which saves the block entity and generates the block id) You are still able to delete and add new media items.
Proposed resolution
Figure out how to add an access dependency to the content block, before the node it's used in is saved. Or somehow figure out a way to bypass whatever is checking for this until we are able to set a dependency.
I am not sure how to resolve this and could use some insight/guidance.