Preprocess function not working with Layout Builder (Drupal 8.9.13)

195 Views Asked by At

I use Layout Builder on a Drupal 8 project. I created a few components, some views blocks and some content blocks (made with custom block types from the back office). In the Layout Builder UI, it is possible to add blocks already created or to add a new custom content block.

I have no issue with any kind of block that is reused, they appear just fine. When I create a new custom content block in the modal, two things can happen :

  1. If I don't use a preprocess function for that type of block in the .theme, the block will be displayed without any issue.
  2. If I use a preprocess function for that type of block in the .theme, the block won't be displayed and I get an Ajax error :
" Une erreur HTTP AJAX s'est produite.
Code de statut HTTP : 200
Informations de débogage ci-dessous. 
Chemin : [...]/layout_builder/add/block/overrides/node.254/0/content/inline_block%3Abasic?_wrapper_format=drupal_dialog.off_canvas&ajax_form=1 
StatusText: OK 
ResponseText : AssertionError: Cannot load the "block_content" entity with NULL ID. in assert() (line 249 of [...]\web\core\lib\Drupal\Core\Entity\EntityStorageBase.php)."

After the Ajax error, if I save the node and refresh the page, the block will finally appear. If I trigger an Ajax error with a "preprocessed" content block and then try to add an "non-preprocessed" content block, then I will get another Ajax error and the block won't be display either.

From what I've understand from my intensive Googling, it seems to be related to Drupal Context and I cannot pretend I have a full understanding of how it works. I'm definitely not the first one getting that issue, but I still couldn't find a working solution.

My only solution so far is to get rid of those preprocess functions, but I find it somehow hard to let it go. Any idea or information will be appreciated.

Sorry for my imperfect English and thanks in advance.

0

There are 0 best solutions below