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
- Create a new node type on dev/staging environment
- Enable layout builder for this bundle (no override layouts)
- Create a default layout with inline blocks
- drush config:export, transfer config to live environment, drush config:import
- visit /admin/structure/types/manage/NODE_TYPE/display/default/layout
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #58 | core-3180702-11.3.patch | 12.92 KB | akalam |
| #55 | interdiff.txt | 3.25 KB | unstatu |
| #55 | 3180702-55.patch | 11.94 KB | unstatu |
| #54 | 3180702-2383-10.3.1.patch | 9.08 KB | akalam |
Issue fork drupal-3180702
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
Comment #2
agentrickardFor reference, here is a work-in-progress patch against 9.0.x.
Comment #3
agentrickardComment #5
SpacemanPete commentedSo, 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_lockwe 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 filecore.entity_view_display.node.landing_page.default.ymlends up looking like: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_blockI'd love to see sth like:Am I crazy?
Comment #6
tim.plunkettCustom 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.
Comment #7
SpacemanPete commented@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?
Comment #8
tim.plunkettAhh, yes.
That's the use case that is supported today.
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.
Comment #9
grayle commentedIn 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.
Comment #10
grayle commentedFixed a ?? brainfart
Comment #12
grayle commentedOK, 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.
Comment #13
grayle commentedComment #14
maurizio.ganovelliI'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.
Comment #15
maurizio.ganovelliSorry, last patch contains some lines of code of another patch applied.
Comment #16
aditya.ghan commentedPatch #15 works for me, allows to load inline blocks through its UUID
Comment #17
aditya.ghan commentedI 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.
Comment #18
aditya.ghan commentedFixed PHPsniffs and reuploading the patch
Comment #19
grayle commented@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.
Comment #21
aaron.ferris commentedEcho'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.
Comment #22
aaron.ferris commentedI'm working on a patch for this, what I'm seeing locally is
block_revision_idis 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()vsconfiguration['block_uuid']as recommended above, for an additional check which feels logical.Comment #23
aaron.ferris commentedWould 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.
Comment #25
ok4p1 commentedHello @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.
Comment #26
ok4p1 commented@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.
Comment #27
ok4p1 commentedRemoving the .txt from the patch...
Comment #28
ok4p1 commentedComment #29
ok4p1 commentedI should have tested it more, removing the patch I added since with a fresh install is giving me an error, apologies...
Comment #30
ok4p1 commentedI 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.
Comment #31
aaron.ferris commentedHello @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
Comment #32
ok4p1 commentedHello @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.
Comment #33
joachim commentedWorking on tests for this.
Comment #36
joachim commentedAdded tests, made a MR for the fix and a tests-only MR.
Comment #39
joachim commentedOops! Forgot to clean up test notes.
Comment #41
smustgrave commentedThis 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.
Comment #43
nginex commentedI 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
Comment #45
tim.plunkett@nginex I rebased the MR (which anyone can do) and @joachim changed the target (which I asked for in Drupal slack)
Comment #46
nginex commented@tim.plunkett, right, I will take this into account next time, thanks
Comment #47
smustgrave commentedPreviously tagged for update path for the new schema item.
If not needed can we document why not please.
Looking good though!
Thanks!
Comment #48
joachim commentedComment #49
alt.dev commentedI'm attaching a patch based on the latest MR that can be applied to Drupal 10.2.x.
Comment #50
burak.ikiler commentedCurrent d10-49 patch is failing with 10.2.3. I am adding the compatible one.
Comment #51
pratikshad commentedThanks for sharing the patch, it worked for me
https://git.drupalcode.org/project/drupal/-/merge_requests/2383/diffs
Comment #52
tonibarbera commentedI've rerolled the #50 patch for the Drupal 10.3.1 version
Comment #53
akalam commentedPatches #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
Comment #54
akalam commentedAs 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.
Comment #55
unstatu commentedI 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)
Comment #56
aaronchristian commentedI 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).Comment #57
nojj commentedI 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 ?
Comment #58
akalam commentedRerolled against 11.3
Comment #61
rodrigoaguileraRerroll by merging main.
I was not able to reproduce the error that aaronchristian describes so setting back to NR