Problem/Motivation

Currently, if you create custom blocks inside a Layout Builder default layout, those blocks are exported with their revision_id as the key. This can lead to errors when syncing config across environments -- even when those blocks are auto-created via migration or some other method.

Steps to reproduce

  1. Create a new node type on dev/staging environment
  2. Enable layout builder for this bundle (no override layouts)
  3. Create a default layout with inline blocks
  4. drush config:export, transfer config to live environment, drush config:import
  5. visit /admin/structure/types/manage/NODE_TYPE/display/default/layout
  6. WSOD due missing inline blocks, even without overridden layouts

This issue can be mitigated by #3049332: Fix Log error + visual warning for missing or broken block and by exporting blocks with Default Content #2942975: [PP-1] Expose Layout Builder data to REST and JSON:API. However, we cannot guarantee that the revision_id will match when the blocks are exported/imported.

Proposed resolution

Export the 'block_uuid' as part of the configuration, and use it for block loading if it is present.

Remaining tasks

- Needs a patch that is 9.2.x compatible
- Needs tests

User interface changes

- None

API changes

- None

Data model changes

- Adds "block_uuid" to the Layout Builder schema.

Release notes snippet

Issue fork drupal-3180702

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

agentrickard created an issue. See original summary.

agentrickard’s picture

StatusFileSize
new2.21 KB

For reference, here is a work-in-progress patch against 9.0.x.

agentrickard’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 3180702-layout_builder-uuid copy.patch, failed testing. View results

SpacemanPete’s picture

So, I've never quite understood why Default Blocks get exported to the LB Region config essentially as content entities with UUIDs and everything.

We've run into this issue in our build where we'll build a Custom Block Type and place it in a LB section as a Default Block so that when Content Editors create new nodes of a given CT, they've already got the first one or two blocks there and just need to enter text/images to the fields. But when we try to port this work to a target site and import the config, we run into an error since the content entity by UUID doesn't exist on the target env.

Since Custom Block Types can be exported/imported as configuration entities, wouldn't it just make sense to define Default Blocks in Layout Builder as configuration entities instead of a content entities?

For example, we created a Landing Page Node CT, and using layout_builder_lock we made a restricted Hero section with a Hero Custom Block Type that editors will need to fill out but they won't be able to remove from that hero region or add other block to that region. The Config file core.entity_view_display.node.landing_page.default.yml ends up looking like:

uuid: ae98f153-89a5-4290-8166-16bf16fd20a8
langcode: en
status: true
dependencies:
  config:
    - block_content.type.hero_block
    - field.field.node.landing_page.layout_builder__layout
    - node.type.landing_page
  module:
    - layout_builder
    - layout_discovery
    - user
third_party_settings:
  layout_builder:
    allow_custom: true
    enabled: true
    sections:
      -
        layout_id: layout_onecol
        layout_settings:
          label: 'Hero section'
        components:
          82387f49-3f0e-4e3e-b9ac-0b0c61de6738:
            uuid: 82387f49-3f0e-4e3e-b9ac-0b0c61de6738
            region: content
            configuration:
              id: 'inline_block:hero_block'
              label: 'Inline block'
              provider: layout_builder
              label_display: ''
              view_mode: full
              block_revision_id: 11
              block_serialized: null
              context_mapping: {  }
            additional: {  }
            weight: 0
        third_party_settings:
          layout_builder_lock:
            lock:
              2: 2
              3: 3
              4: 4
              5: 5
              6: 6
              8: 8
      -
        layout_id: layout_onecol
        layout_settings:
          label: 'Main section'
        components: {  }
        third_party_settings:
          layout_builder_lock:
            lock: {  }
id: node.landing_page.default
targetEntityType: node
bundle: landing_page
mode: default
content: {  }
hidden:
  layout_builder__layout: true
  links: true

Wouldn't it be amazing to have the component key in a given section just store an array of config entities by name that could then generate the appropriate inline blocks on Node creation?
Since our custom Block Type has the machine name of hero_block I'd love to see sth like:

    ...
    sections:
      -
        layout_id: layout_onecol
        layout_settings:
          label: 'Hero section'
        components:
          hero_block
        third_party_settings:
          layout_builder_lock:
            lock:
              2: 2
              ...

Am I crazy?

tim.plunkett’s picture

Custom Block Types are to Custom Blocks
as
Content Types are to Nodes

The first is always a config entity, the second is always a content entity.

SpacemanPete’s picture

@tim.plunkett, great simple analogy =)

Which is why I would anticipate that defining default blocks for Content Type config files would store Custom Block Type config entity definitions in the components array, not Custom Block content entities.

That way when a User creates a new Node and start editing the Layout Builder there would already be an empty Hero Block with empty fields for them to fill out.

I guess I can see the value of storing a content entity as a default block for instances when we would want a new Node of a given Content Type to already include some fields filled out.

So, as I understand it, there's currently no way to do the former?

tim.plunkett’s picture

Ahh, yes.

storing a content entity as a default block for instances when we would want a new Node of a given Content Type to already include some fields filled out.

That's the use case that is supported today.

when a User creates a new Node and start editing the Layout Builder there would already be an empty Hero Block with empty fields for them to fill out.

You could accomplish this with custom code (subscribing to \Drupal\layout_builder\Event\PrepareLayoutEvent, currently only available in 9.1.0), or open a new feature request.

grayle’s picture

StatusFileSize
new2.62 KB
new1.75 KB

In SaveBlockContent it was still ignoring the load by UUID option, so I added it.

I wanted to just call getEntity there as well, but maybe there's a reason all the code is semi-duplicated in there so I left it.

grayle’s picture

StatusFileSize
new1.41 KB
new2.63 KB

Fixed a ?? brainfart

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

grayle’s picture

OK, found a fairly big issue, or rather something I feared I also actually ran into now.

Loading by UUID first basically means if the UUID is set it'll always load the default revision of the block.
Not loading by UUID first basically makes the patch useless, as it will then try to load a revision which may not exist, or does exist but is an entirely different block altogether. So back to square one.

I don't know how to cleanly fix this. We can start by loading on rev ID and then checking the loaded block's UUID to the UUID present in configuration, etc etc. But we should probably brainstorm exactly how to handle it before I start doing random stuff.

For now I'm going to make a patch which only exports UUIDs but doesn't even look at it for loading. That way I can manually fix stuff in a post migrate event.

grayle’s picture

StatusFileSize
new1.22 KB
new1.56 KB
maurizio.ganovelli’s picture

StatusFileSize
new2.88 KB

I'm working on this patch in conjunction with default_content module.
I'm trying to load blocks by uuid as "second chance": if revision_id doesn't match with an existing block, check if a block with same uuid exists before give up (getEntity method).
I need to test more but seems to work.

maurizio.ganovelli’s picture

StatusFileSize
new2.68 KB

Sorry, last patch contains some lines of code of another patch applied.

aditya.ghan’s picture

Patch #15 works for me, allows to load inline blocks through its UUID

aditya.ghan’s picture

StatusFileSize
new2.78 KB

I have been testing Patch 15 extensively. Found out that it will not work in this case:

If we have blocks imported into a new environment using default content and if we use those block UUIDs in your layout builder, the system would load unrelated blocks using the revision ID because those revision IDs correspond to different blocks in the new environment.

I believe we should prioritize loading inline blocks through UUIDs first and then the revision ID. I have uploaded the patch which does it.

aditya.ghan’s picture

StatusFileSize
new2.78 KB

Fixed PHPsniffs and reuploading the patch

grayle’s picture

@aditya.ghan that's what an older version of the patch did. It won't work, UUIDs aren't revisionable. It will therefore always load the default revision of the block, breaking older/future/draft revisions.

What we can do is:

- load by revision ID
- if it fails, load by UUID
- it it doesn't fail, check the loaded block's UUID to the stored UUID in plugin config. If they do NOT match, load the block by UUID anyway because clearly we've loaded a wrong block.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

aaron.ferris’s picture

Echo'ing @Grayles findings on this, the patch means revisions of block content won't be loaded which is a problem. Word of warning for anyone else who might be considering the patches attached to this ticket.

aaron.ferris’s picture

I'm working on a patch for this, what I'm seeing locally is block_revision_id is always populated for default content blocks but the entity returned from $entity = $this->entityTypeManager->getStorage('block_content')->loadRevision($this->configuration['block_revision_id']); is empty.

Thus, my approach is to check against an empty $entity object and build from UUID if it's present. I'm also looking to check against $entity->uuid() vs configuration['block_uuid'] as recommended above, for an additional check which feels logical.

aaron.ferris’s picture

StatusFileSize
new2.21 KB

Would the attached work? Seems to work in my (limited) testing, but does seem to allow default content to export blocks via UUID and revisioning of blocks in layout builder.

Feedback welcome. I'm fortunate in that my project will be getting some extensive QA eyes on this, so will update based on their output.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ok4p1’s picture

Hello @aaron.ferris, how did your QA go? I have tried your patch and the default custom blocks are loaded correctly, but when I save the node without overriding the custom block I get the wrong custom block saved.

ok4p1’s picture

StatusFileSize
new2.21 KB

@aaron.ferris I updated your patch to incorporate @Grayle's 3rd suggestion about comparing the loaded block's UUID to the stored UUID. I haven't tested it much but it seems to have resolved my issue.

ok4p1’s picture

StatusFileSize
new2.21 KB

Removing the .txt from the patch...

ok4p1’s picture

ok4p1’s picture

I should have tested it more, removing the patch I added since with a fresh install is giving me an error, apologies...

ok4p1’s picture

StatusFileSize
new2.46 KB

I had more time to test the following patch and it is working for me. My use case is the following:
- I added an inline-block to a content type in layout builder
- I exported the config and grabbed the block block_uuid
- I exported the block with the module default_content
- Pushed the code to a new environment, the inline blocks get recreated and revision is working.

Without the change on my patch, in the new environment I was getting random blocks on node save if I didn't override right away the inline blocks.

aaron.ferris’s picture

Hello @Ok4p1

QA for my patch wasn't ideal in honesty, we were seeing problems with blocks being pulled in incorrectly (which does seem to correlate with your results of my patch?), however I haven't had a chance to take another look at this yet

ok4p1’s picture

Hello @aaron.ferris, you can give it a try to the changes I made on top of your patch, so far it looks like they are working fine but would like more people to test it.

joachim’s picture

Assigned: Unassigned » joachim

Working on tests for this.

joachim’s picture

Status: Needs work » Needs review

Added tests, made a MR for the fix and a tests-only MR.

The last submitted patch, 23: 3180702-layout_builder-uuid-copy-23.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 30: 3180702-layout_builder-uuid-copy-30.patch, failed testing. View results

joachim’s picture

Status: Needs work » Needs review

Oops! Forgot to clean up test notes.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Can the MR be updated for 10.1 please
Left a comment, but since it's updating the schema believe it will need an update hook and thus test for update hook.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nginex’s picture

Status: Needs work » Needs review
StatusFileSize
new6.12 KB
new8.89 KB

I cound not create a new MR against 10.1.x as I'm not owner of the existing MR.

But I rerolled existing MR against 10.1.x

The last submitted patch, 43: 3180702-d10-43-TEST-ONLY-FAIL.patch, failed testing. View results

tim.plunkett’s picture

@nginex I rebased the MR (which anyone can do) and @joachim changed the target (which I asked for in Drupal slack)

nginex’s picture

@tim.plunkett, right, I will take this into account next time, thanks

smustgrave’s picture

Status: Needs review » Needs work

Previously tagged for update path for the new schema item.

If not needed can we document why not please.

Looking good though!

Thanks!

joachim’s picture

Assigned: joachim » Unassigned
alt.dev’s picture

StatusFileSize
new39.5 KB

I'm attaching a patch based on the latest MR that can be applied to Drupal 10.2.x.

burak.ikiler’s picture

StatusFileSize
new35.98 KB

Current d10-49 patch is failing with 10.2.3. I am adding the compatible one.

pratikshad’s picture

Thanks for sharing the patch, it worked for me
https://git.drupalcode.org/project/drupal/-/merge_requests/2383/diffs

tonibarbera’s picture

StatusFileSize
new33.05 KB

I've rerolled the #50 patch for the Drupal 10.3.1 version

akalam’s picture

StatusFileSize
new39.05 KB

Patches #50 and #52 are not including the function ViewsConfigUpdater::needsTidFilterWithMultipleVocabulariesUpdate which is being used on the hook post update.
I'm uploading a reroll against 10.3.1 based on patch #49

akalam’s picture

StatusFileSize
new9.08 KB

As reported in the previous comment, patches #50 and #52 were causing a fatal error, so I was creating a new one based on the patch #49. However, after testing, seems like the patch #49 was incorrect too, because is not loading the entity from uuid on the getEntity method as it's being done in the MR, and therefore the patch #53 is incorrect as well.

I'm uploading a direct reroll from the MR against core 10.3.1 in case anyone needs it.

unstatu’s picture

StatusFileSize
new11.94 KB
new3.25 KB

I have upgraded the patch from #54 in order to allow to the inline block dependency event subscriber to recognize the inline blocks that are referenced with a uuid in the layout.

This is important in order to calculate the correct access checks (\Drupal\block_content\BlockContentAccessControlHandler)

aaronchristian’s picture

I tired the patch and it works well for exporting the uuid in the default layout builder layout (entity_view_display).

However, here's what im experiencing;

After viewing a different node with the default layout active, editting the inline block and then saving the page results in the following error.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '125' for key 'PRIMARY': INSERT INTO "inline_block_usage" ("block_content_id", "layout_entity_id", "layout_entity_type") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 125 [:db_insert_placeholder_1] => 2895 [:db_insert_placeholder_2] => node ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of /var/www/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

nojj’s picture

I am having this issue on a Drupal 10.4 site.

is there any progress here? or should I test patch #55?

@aaronchristian : could you fix your issue ?

akalam’s picture

StatusFileSize
new12.92 KB

Rerolled against 11.3

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

rodrigoaguilera made their first commit to this issue’s fork.

rodrigoaguilera’s picture

Status: Needs work » Needs review

Rerroll by merging main.

I was not able to reproduce the error that aaronchristian describes so setting back to NR