Error with media library in Gutenberg Content Block: "Non-reusable blocks must set an access dependency for access control"

Created on 13 July 2024, 5 months ago
Updated 26 August 2024, 3 months ago

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

  1. Create a content block that has a media field and uses the media library widget for selecting media.
  2. Enable this block type in gutenberg
  3. Create a new gutenberg node
  4. Add the content block with the media field.
  5. Add media to the blocks field.
  6. Click the update block button (this saves the non-reusable content block)
  7. edit the content block again, this time remove a media item, and try adding a new media item.
  8. 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.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @loze
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles
  • Pipeline finished with Success
    5 months ago
    Total: 204s
    #223210
  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    I think I was able to resolve this. What I ended up doing is

    1. Added a column to the content block usage table for the parent gutenberg node bundle
    2. Altered the gutenberg.content_block_type.settings to pass along the parent entities id, type, and bundle so these can be saved to the usage table as soon as the block is created
    3. Created an eventSubscriber for BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY which will check the usage and return the node the block is used in, or create an empty node if the parent node is new and doesn't have an id yet,

    This appears to allow editing of media fields of content blocks using the media library now.

  • Pipeline finished with Canceled
    4 months ago
    Total: 86s
    #243665
  • Pipeline finished with Success
    4 months ago
    Total: 203s
    #243666
  • Pipeline finished with Success
    4 months ago
    Total: 213s
    #251375
  • Status changed to Fixed 4 months ago
  • Pipeline finished with Success
    4 months ago
    Total: 339s
    #254244
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 212s
    #307145
  • Pipeline finished with Failed
    about 2 months ago
    Total: 191s
    #307151
  • Pipeline finished with Success
    about 2 months ago
    Total: 265s
    #307286
  • Pipeline finished with Failed
    about 2 months ago
    Total: 579s
    #307803
  • Pipeline finished with Failed
    about 2 months ago
    Total: 386s
    #307818
  • Pipeline finished with Success
    about 2 months ago
    Total: 2265s
    #307835
  • Pipeline finished with Failed
    30 days ago
    #329065
  • Pipeline finished with Success
    29 days ago
    Total: 550s
    #329669
  • Pipeline finished with Failed
    29 days ago
    Total: 622s
    #329676
  • Pipeline finished with Success
    21 days ago
    Total: 167s
    #336866
  • Pipeline finished with Success
    21 days ago
    Total: 179s
    #336878
  • Pipeline finished with Failed
    10 days ago
    Total: 402s
    #348590
  • Pipeline finished with Success
    5 days ago
    Total: 542s
    #354694
  • Pipeline finished with Success
    4 days ago
    Total: 437s
    #354704
Production build 0.71.5 2024