• Install module from scratch
  • Enable autocomplete in config
  • Save block with classes
Uncaught PHP Exception TypeError: "array_merge(): Argument #1 must be of type array, null given" at /web/modules/contrib/block_class/src/Service/BlockClassHelperService.php line 202
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

VladimirAus created an issue. See original summary.

vladimiraus’s picture

Priority: Normal » Major
Status: Active » Needs review

Good to go!

chris matthews’s picture

chris matthews’s picture

Status: Needs review » Reviewed & tested by the community

Manually tested by:
Installing drupal 10.0.9
Installing block_class (dev-2.0.x b1f185f)
(At /admin/config/content/block-class/settings, 'Enable auto-complete' was already enabled by default)
Adding a class to a block resulted in:

TypeError: array_merge(): Argument #1 must be of type array, null given in array_merge() (line 193 of /code/web/modules/contrib/block_class/src/Service/BlockClassHelperService.php).

Repeating above but with the patch applied did not throw this error so confidently setting to RTBC.

dydave’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Reviewed & tested by the community » Needs review

Thanks everyone for creating this ticket and your code contributions, it's greatly appreciated!

I've taken a quick look at this issue and tried repeating the steps described in the IS and at #5 but unfortunately I was unable to reproduce the problem and prompt the PHP error message:
No crash: Everything worked fine/as expected

Tested with:

  • drupal/core: 10.2.5
  • drupal/block_class: 'dev-2.0.x:e03fb6a61296eb8ae595bfc8298e33e633a57610'

 
Step 1: Install module block_class from scratch (no prior config):

> drush en block_class
 [success] Successfully enabled: block_class

> drush cget block_class.settings

_core:
  default_config_hash: XEpuHrBp3DELKxVx0tmwE6CS7NSIKXu4F6QhT_vwqd0
block_classes_stored: {  }
default_case: standard
enable_attributes: true
enable_auto_complete: true
enable_id_replacement: true
enable_special_chars: false
field_type: multiple_textfields
filter_html_clean_css_identifier: ''
items_per_page: 50
qty_attributes_per_block: 10
qty_classes_per_block: 10
maxlength_attributes: 255
maxlength_block_class_field: 255
maxlength_id: 255
weight_attributes: 0
weight_class: 0
weight_id: 0

As you can see block_classes_stored: { } is an array, so it seems it "should" have an empty array [] as its default value and not null.

Additionally, is seems auto-complete is enabled by default as well: enable_auto_complete: true.

See current: block_class.settings.yml
https://git.drupalcode.org/project/block_class/-/blob/e03fb6a61296eb8ae5...
 

Step 2: Browse to "Main page content" (system_main_block) edit form:
/admin/structure/block/manage/olivero_content
 

Step 3: Add a class in autocomplete field "CSS class", for example: 'testingBC1', then save.
 

I'm not sure if I'm missing anything here, but with a fresh 'drupal/block_class:2.0.x-dev@dev' module install I was not able to reproduce the error.
 
Could someone please take another look at this issue and let us know if the problem is still affecting the latest 2.0.x DEV code base?

For reference, it seems the default value of the block_classes_stored config was updated in #3338774: Storing block_classes_stored as a string value leads to poor developer experience.
 

Lastly, I'd like to point out the steps outlined above should actually be the same ones executed in module's Functional tests for the Create ("add") block form: /admin/structure/block/add/system_main_block/olivero.
which all seem to be passing at the moment: 🟢

Code in test class:
https://git.drupalcode.org/project/block_class/-/blob/e03fb6a61296eb8ae5...
 

We would greatly appreciate if anyone could help confirming the problem in this ticket has been resolved in current DEV, or if it is still active, in which case, more contextual information would be very helpful.

Thanks in advance for your tests, reporting, comments and feedback.

vladimiraus’s picture

Issue tags: +PHP 8.2, +PHP 8.3

@DYdave which php version are you testing against?
Should be 8.2+

dydave’s picture

Thanks a lot Vladimir (@VladimirAus) and sorry for the late reply.

OK, I've given this another round of tests and still no luck reproducing the issue 😭

Fresh Drupal 11 install on a DDEV stack:

  • Drupal version : 11.0-dev
  • Block Class: 2.0.x-dev (from HEAD)

 
Enabled via drush, same config as above at #6 (DEFAULT):
 
PHP 8.3 :

/var/www/html$ php -v
PHP 8.3.8 (cli) (built: Jun  8 2024 21:31:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.8, Copyright (c), by Zend Technologies

Same steps followed as above at #6:

Step 2: Browse to "Main page content" (system_main_block) edit form:
/admin/structure/block/manage/olivero_content

Step 3: Add a class in autocomplete field "CSS class", for example: 'testingBC1', then save.

No error, the form was submitted properly, value saved and block class displayed....

Additionally, I'd like to point out 2.0.x has OPT_IN_TEST_MAX_PHP: 1, so all the functional tests should be running for PHP 8.3, see the following pipeline:
https://git.drupalcode.org/issue/block_class-3345203/-/pipelines/249232
MAX PHP 8.3 on D10.3.3-dev job : https://git.drupalcode.org/issue/block_class-3345203/-/jobs/2396338

All passing 🟢

Vladimir (@VladimirAus), I really don't know what else I could do here ...

I would really like to help testing this and I've already spent quite some time trying to reproduce the issue ...

On top of that, I'm rather surprised the problem isn't getting more traction, in the sense: We're not getting more reports or feedback on this, when the problem should be pretty critical and jump out to users on first use of the module/class config.

If you have any suggestions on how I could be reproducing this, I'd be very grateful!

Currently, I'm on a DDEV stack, fresh D10 or D11 install, PHP 8.3, block_class 2.0.x HEAD

Thanks in advance !

vladimiraus’s picture

Status: Needs review » Needs work

Here's what I have on

  • Block Class 2.0.12
  • Drupal 10.3.2
  • PHP 8.3

After dragging blocks around and saving blocks:

The website encountered an unexpected error. Try again later.

TypeError: array_merge(): Argument #1 must be of type array, string given in array_merge() (line 202 of modules/contrib/block_class/src/Service/BlockClassHelperService.php).
Drupal\block_class\Service\BlockClassHelperService->blockClassPreSave(Object) (Line: 107)
block_class_block_presave(Object)
call_user_func_array(Object, Array) (Line: 416)
Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object, 'block_class') (Line: 395)
Drupal\Core\Extension\ModuleHandler->invokeAllWith('block_presave', Object) (Line: 415)
Drupal\Core\Extension\ModuleHandler->invokeAll('block_presave', Array) (Line: 341)
Drupal\Core\Config\Entity\ConfigEntityStorage->invokeHook('presave', Object) (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 257)
Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 614)
Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 392)
Drupal\block\BlockListBuilder->submitForm(Array, Object)

If you cannot reploduce it on the fresh one, it might be bad update procedure from Drupal 9.

vladimiraus changed the visibility of the branch 3345981-initial-class-save to hidden.

vladimiraus changed the visibility of the branch 3345981-initial-class-save to active.

vladimiraus’s picture

Status: Needs work » Needs review
jannakha’s picture

Status: Needs review » Needs work

MR#12 doesn't work in my case where configuration is not stored as expected:

bloc_class_settings.yml contains:
block_classes_stored: '{"social-icons-inline":"social-icons-inline","container":"container","block-below":"block-below","popup-services-browser":"popup-services-browser","popup-buildings-browser":"popup-buildings-browser","pull-right":"pull-right","block-blog-tags":"block-blog-tags","block-banner-banner":"block-banner-banner","block-solutions-back-button":"block-solutions-back-button","col-md-6":"col-md-6","col-lg-60":"col-lg-60","clear-xs":"clear-xs","clear-sm":"clear-sm","floating-links-2":"floating-links-2","padding-large-md-right":"padding-large-md-right","padding-none-xs-right":"padding-none-xs-right","padding-none-xs-left":"padding-none-xs-left","block-small-margin":"block-small-margin","search-form-block":"search-form-block","col-xs-5":"col-xs-5","col-sm-3":"col-sm-3","col-lg-20":"col-lg-20","padding-lg-right":"padding-lg-right","view-keyword-search-filters":"view-keyword-search-filters","block-covid-19-message":"block-covid-19-message","block-front-cta":"block-front-cta","col-sm-5":"col-sm-5","col-md-4":"col-md-4","block-360-solutions-video-popup":"block-360-solutions-video-popup","destroy-on-close":"destroy-on-close","":"","request-quote-link":"request-quote-link","d-md-inline-block":"d-md-inline-block","w-md-50":"w-md-50"}'

is my config old or wrong?

jannakha’s picture

Status: Needs work » Closed (outdated)

updb didn't run properly update hook block_class_update_20017() which updates config structure

stewest’s picture

simsimsim101’s picture

Issue summary: View changes

Is this issue has been resolved? Whenever I try to modify a block (title, description, position in the layout, etc..) I get the same error from Mr#9 :

TypeError: array_merge(): Argument #1 must be of type array, string given in array_merge() (line 202 of modules/contrib/block_class/src/Service/BlockClassHelperService.php).

I tried the command "drush cget block_class.settings" and the block_classes_stored does not contain any array's but is instead kept in string:
block_classes_stored: '{"col-sm-4":"col-sm-4","col-md-3":"col-md-3","col-sm-offset-2":"col-sm-offset-2","col-md-offset-4":"col-md-offset-4","col-lg-offset-5":"col-lg-offset-5","col-sm-6":"col-sm-6","col-md-5":"col-md-5","col-lg-4":"col-lg-4","col-md-12":"col-md-12","col-sm-7col-lg-6":"col-sm-7col-lg-6","col-sm-3":"col-sm-3","col-lg-offset-3":"col-lg-offset-3","col-sm-12":"col-sm-12"}'
It seems like the " $current_block_classes = explode(' ', $block_classes ?? ''); " doesn't convert the String value to an array correctly...
Here's the solutions that I tried by following this comment thread:
-> Change the line 196 of BlockClassHelperService.php to " $block_classes_stored = $config->get('block_classes_stored') ?? [];"
-> The Commands "drush cr" + "drush updb".
-> Starting the local environment to a clean build (started over from scratch with 0 file modified from the origin).

Did anyone find a solution to the error from Mr#9 ?

simsimsim101’s picture

Issue summary: View changes
liam morland’s picture

I have seen this on sites that did not have any outstanding update hooks. It was fixed by a cache clear. This is probably a bug that ought to be fixed.

hanan alasari’s picture

MR#12 worked for me, thank you!!

ronraney’s picture

I'm seeing this every time I save the Block Layout page.

The website encountered an unexpected error. Try again later.

TypeError: array_merge(): Argument #1 must be of type array, string given in array_merge() (line 202 of modules/contrib/block_class/src/Service/BlockClassHelperService.php).

#0 /app/web/modules/contrib/block_class/src/Service/BlockClassHelperService.php(202): array_merge()
#1 /app/web/modules/contrib/block_class/block_class.module(107): Drupal\block_class\Service\BlockClassHelperService->blockClassPreSave()
#2 [internal function]: block_class_block_presave()
#3 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(416): call_user_func_array()
#4 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(395): Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}()
#5 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(415): Drupal\Core\Extension\ModuleHandler->invokeAllWith()
#6 /app/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(341): Drupal\Core\Extension\ModuleHandler->invokeAll()
#7 /app/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(529): Drupal\Core\Config\Entity\ConfigEntityStorage->invokeHook()
#8 /app/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(483): Drupal\Core\Entity\EntityStorageBase->doPreSave()
#9 /app/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(257): Drupal\Core\Entity\EntityStorageBase->save()
#10 /app/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Config\Entity\ConfigEntityStorage->save()
#11 /app/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php(617): Drupal\Core\Entity\EntityBase->save()
#12 /app/web/core/modules/block/src/BlockListBuilder.php(392): Drupal\Core\Config\Entity\ConfigEntityBase->save()
#13 [internal function]: Drupal\block\BlockListBuilder->submitForm()
#14 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array()
#15 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers()
#16 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm()
#17 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm()
#18 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(224): Drupal\Core\Form\FormBuilder->buildForm()
#19 /app/web/core/modules/block/src/BlockListBuilder.php(114): Drupal\Core\Form\FormBuilder->getForm()
#20 /app/web/core/modules/block/src/Controller/BlockListController.php(50): Drupal\block\BlockListBuilder->render()
#21 [internal function]: Drupal\block\Controller\BlockListController->listing()
#22 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#23 /app/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#24 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#25 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#26 /app/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#27 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#28 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#29 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#30 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#31 /app/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#32 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#33 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass()
#34 /app/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle()
#35 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#36 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#37 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#38 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#39 /app/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#40 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#41 {main}
ronraney’s picture

I have the patch installed. I'm now seeing this when I run composer update or install.

drupal/block_class has modified files:
M src/Service/BlockClassHelperService.php
Discard changes [y,n,v,d,s,?]? n
Install of symfony/string failed
Install of symfony/polyfill-php81 failed
Install of symfony/serializer failed
Install of symfony/routing failed
Install of symfony/http-foundation failed
Install of symfony/mime failed
Install of symfony/mailer failed
Install of symfony/var-dumper failed
Install of laminas/laminas-escaper failed
Install of laminas/laminas-feed failed

rcodina’s picture

Status: Closed (outdated) » Needs review

This issue is still present in version 4.0.1. Applying the patch from MR#12 worked for me as well, same as for user #19. Thanks!

@ronraney This occurs because you have a Git sub-repository — it usually happens when a dev version is required via Composer. Removing the module manually and reinstalling a tagged release should resolve it.

dydave’s picture

OK Roger (@rcodina) 👌

I'm going to accept patch from MR !12 👍

I'll get this merged later today, which should be included in 4.0.2.

Thanks again Roger for reporting back on this issue.

dydave’s picture

Version: 2.0.x-dev » 4.0.x-dev
Category: Support request » Bug report

Updated version to current 4.0.x development branch.

dydave’s picture

Status: Needs review » Fixed

Quick follow-up on this issue:

It's been active for a long time, received reasonable testing feedback, reviews and the changes are so small/minor, without much impact to be expected on the module, that it would be a shame not to just get them in the module 😅

Sure that's adding code that may not be absolutely necessary (initialization of config, schema, and such)... there might still be edge cases, depending on projects, where this issue might occur...

Since it should do no harm to the project to add a few additional init checks here and there, I went ahead and merged the changes above at #25. 🥳

This patch should be released very shortly with the next stable, which should hopefully prevent this issue from occurring again in the future.

Feel free to let us know if you have any questions or concerns on any aspects of the latest code changes or the module in general, we would surely be glad to hear your feedback. 😊
Thanks in advance!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.