Fails show the form of adding block to a category PHP TypeError

Created on 16 February 2023, over 1 year ago
Updated 3 May 2023, about 1 year ago

Thank you for this great module. It changed the UX of creating layouts for our custom pages.

Problem/Motivation

Can not Place blocks into a block category due to PHP TypeError.

TypeError: Unsupported operand types: array + null in file /app/web/core/lib/Drupal/Core/Render/Element/Select.php on line 152
Stack trace:
  1. TypeError->() /app/web/core/lib/Drupal/Core/Render/Element/Select.php:152
  2. Drupal\Core\Render\Element\Select->processSelect() /app/web/core/lib/Drupal/Core/Form/FormBuilder.php:1010
  3. call_user_func_array() /app/web/core/lib/Drupal/Core/Form/FormBuilder.php:1010
  4. Drupal\Core\Form\FormBuilder->doBuildForm() /app/web/core/lib/Drupal/Core/Form/FormBuilder.php:1073
  5. Drupal\Core\Form\FormBuilder->doBuildForm() /app/web/core/lib/Drupal/Core/Form/FormBuilder.php:577
  6. Drupal\Core\Form\FormBuilder->processForm() /app/web/core/lib/Drupal/Core/Form/FormBuilder.php:323
  7. Drupal\Core\Form\FormBuilder->buildForm() /app/web/core/lib/Drupal/Core/Controller/FormController.php:73
......

Steps to reproduce

There must be no custom blocks present
1. I install the module.
2. Go to Block categories screen. `/admin/config/content/layout-builder-browser/categories`
3. Add two categories.
4. Go to Blocks tab `/admin/config/content/layout-builder-browser`
5. Once click on Place block on any category it fails with the shared error code

Proposed resolution

The error will not show on the new Drupal installation. it is probably due to some other modules that I'm using.
BUT, the type error fails on the Layout builder browser side. so we can fix it by adding an empty array as a default value for the $block_id to ensure it will always be an array.
The solution can be done on `src/Entity/LayoutBuilderBrowserBlock.php` line 62

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΈπŸ‡¦Saudi Arabia samaphp Riyadh, SA πŸ‡ΈπŸ‡¦

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

Comments & Activities

  • Issue created by @samaphp
  • πŸ‡ΈπŸ‡¦Saudi Arabia samaphp Riyadh, SA πŸ‡ΈπŸ‡¦

    The attached patch should add a default value for $block_id to insure the variable will always be an array.

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΈπŸ‡¦Saudi Arabia samaphp Riyadh, SA πŸ‡ΈπŸ‡¦
  • Status changed to Needs work over 1 year ago
  • πŸ‡«πŸ‡·France vbouchet

    Hi @samaphp.

    As you mentioned, it is not possible to reproduce on a fresh install. Also, the $block_id is defined as a string, not as an array so if we really need to have a default value (which I am not sure), it should probably be a string instead. I feel if we need to fix something, it is probably more in BlockForm.php (providing the form displayed after clicking the "Place block" button) to add check somewhere (but not sure where as I don't reproduce).

  • πŸ‡ͺπŸ‡ΈSpain alexahumada

    Maybe issue 3351162 is related to this.

    That issue can't be reproduced on a fresh install, you need at least one extra language set to default (in my case ES set as default language and EN as secondary) and it's not caused by other installed modules.

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi,

    I am also experiencing the error.

    Patch from comment 2 is not the solution as otherwise the select field will be empty.

    The problem comes from layout_builder_browser/src/Form/BlockForm.php:

      $form['block_id'] = [
        '#suffix' => '</div>',
        '#prefix' => '<div id="block-id-wrapper">',
        '#title' => $this->t('Block'),
        '#type' => 'select',
        '#options' => $blocks[$provider],
        '#default_value' => $browser_block->block_id,
        '#required' => TRUE,
      ];
    

    Initially $provider is empty so $blocks|$provider] is null, so fatal error in Select.php.

    When wrapping in if (!empty($provider) && isset($blocks[$provider])) {, then it is ok. The modal opens, but when choosing a provider, the ajax handling is not correct and the bug occurs again.

  • πŸ‡ͺπŸ‡ΈSpain alexahumada

    grimreaper, take a look at https://www.drupal.org/project/layout_builder_browser/issues/3351162 πŸ› Undefined array key "" in Drupal\layout_builder_browser\Form\BlockForm Closed: duplicate , and try the patch I posted there, maybe it can solve your case temporarily until we find a better solution.

  • πŸ‡¨πŸ‡·Costa Rica robertarias

    I am having the same issue, which only occurs when there are no custom blocks present.

    What @Grimreaper in #6 says is correct, let me explain:

    BlockForm.php tries to set Inline blocks as the default provider, if found within the default providers; if not found, the provider will fallback to an empty string (''). The empty string makes the Block Select element to fail.

    The Select element fails because, when the provider is empty, the #options value will be empty (i.e., no blocks to select), and because we're just creating a new LayoutBuilderBrowserBlock entity, the #default_value will be NULL (i.e., the browser_block->block_id will be NULL - no block has been selected to attach into the entity).

    Within Drupal, when a Select render array is required and the #default_value is empty (among other conditions), an #empty_value is added to the render array to be merged with the #options values (that in this case, will be NULL). However, since no options were set due to an empty provider, PHP fails to merge an array with a NULL value, leading to the error.

    To solve this, I have set the provider to fallback to the first element of the provider_options array. This ensures that if there are no custom blocks, the first provider will be selected, and no error will be thrown because the provider exists and must have at least one block.

    Also, as explain on #4, the LayoutBuilderBrowserBlock->blockId must be a string. While solution #2 may have worked, it is not ideal as the Select element thinks there is a #default_value, which skips the #empty_value and #options merge (besides changing the blockId type).

    #7 is a different issue and its solution does not work for this case.

  • Status changed to Needs review about 1 year ago
  • Status changed to RTBC about 1 year ago
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi,

    Thanks @robert-arias.

    Patch from comment 8 fixes the bug!

  • πŸ‡«πŸ‡·France vbouchet

    Thanks for the patch and the testing. I was able to reproduce and confirm that the patch is fixing the issue. It will be part of the next release.

  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • πŸ‡«πŸ‡·France vbouchet

    This is a comment to see if @Grimreaper gets the credit he deserves for reviewing, suggesting correction and validating the patches in this issue.

Production build 0.69.0 2024