Problem/Motivation

Drupal 9.5.5 was released on: 15 Mar 2023
https://www.drupal.org/project/drupal/releases/9.5.5

Facing fatal error issues after updating to Drupal 9.5.5

Maybe related to changes after the fix for
Drupal core - Moderately critical - Information disclosure - SA-CORE-2023-003

The language module provides a Language switcher block which can be placed to provide links to quickly switch between different languages.
The URL of unpublished translations may be disclosed. When used in conjunction with a module like Pathauto, this may reveal the title of unpublished content.

The block will not show up in many pages.

Steps to reproduce

Given that the "Language Switcher Dropdown" module was installed in a Drupal 9.5.5 site
And placed the language drop down block in a region for the default active theme
And the "Basic Page" content type was configured for content translation
And more languages were added to the system
When adding a "Basic Page" node with "English" as the language
And press the "Save" submit button
Then the following Fatal error will show up

[Wed Mar 15 22:31:44.422064 2023] [php:notice] [pid 1169478] [client 10.50.111.41:35104] TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in /var/www/html/test/drupal9/web/modules/contrib/lang_dropdown/src/Plugin/Block/LanguageDropdownBlock.php on line 726 #0 /var/www/html/test/drupal9/web/modules/contrib/lang_dropdown/src/Plugin/Block/LanguageDropdownBlock.php(726): array_keys()\n#1 /var/www/html/test/drupal9/web/core/modules/block/src/BlockViewBuilder.php(171): Drupal\\lang_dropdown\\Plugin\\Block\\LanguageDropdownBlock->build()\n#2 [internal function]: Drupal\\block\\BlockViewBuilder::preRender()\n#3 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(101): call_user_func_array()\n#4 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(788): Drupal\\Core\\Render\\Renderer->doTrustedCallback()\n#5 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(374): Drupal\\Core\\Render\\Renderer->doCallback()\n#6 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(446): Drupal\\Core\\Render\\Renderer->doRender()\n#7 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\\Core\\Render\\Renderer->doRender()\n#8 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\\Core\\Render\\Renderer->render()\n#9 /var/www/html/test/drupal9/web/sites/default/files/php/twig/64121bc4a90c0_page.html.twig_kr1CGZ6aIrcITbJE5nyBk_3x3/XmVwToDgV8S5wwI412DYRjkeq2vBSeWYl9u6GvLh1RA.php(95): Drupal\\Core\\Template\\TwigExtension->escapeFilter()\n#10 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(405): __TwigTemplate_2af0607cd6e854321751a147533f8ec2->doDisplay()\n#11 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(378): Twig\\Template->displayWithErrorHandling()\n#12 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(390): Twig\\Template->display()\n#13 /var/www/html/test/drupal9/web/core/themes/engines/twig/twig.engine(55): Twig\\Template->render()\n#14 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template()\n#15 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\\Core\\Theme\\ThemeManager->render()\n#16 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\\Core\\Render\\Renderer->doRender()\n#17 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\\Core\\Render\\Renderer->render()\n#18 /var/www/html/test/drupal9/web/sites/default/files/php/twig/64121bc4a90c0_html.html.twig_RZHek83SVcT8gDF9-bPXVpyoF/ng2jxdLBGZs31G0aMmFM8WRG0Hi4agQu0k0rEwGQVVI.php(93): Drupal\\Core\\Template\\TwigExtension->escapeFilter()\n#19 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(405): __TwigTemplate_bc2efd76ab2aceb2ab69f40efb3bdcd7->doDisplay()\n#20 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(378): Twig\\Template->displayWithErrorHandling()\n#21 /var/www/html/test/drupal9/vendor/twig/twig/src/Template.php(390): Twig\\Template->display()\n#22 /var/www/html/test/drupal9/web/core/themes/engines/twig/twig.engine(55): Twig\\Template->render()\n#23 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template()\n#24 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\\Core\\Theme\\ThemeManager->render()\n#25 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\\Core\\Render\\Renderer->doRender()\n#26 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(162): Drupal\\Core\\Render\\Renderer->render()\n#27 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\\Core\\Render\\MainContent\\HtmlRenderer->Drupal\\Core\\Render\\MainContent\\{closure}()\n#28 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(163): Drupal\\Core\\Render\\Renderer->executeInRenderContext()\n#29 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\\Core\\Render\\MainContent\\HtmlRenderer->renderResponse()\n#30 [internal function]: Drupal\\Core\\EventSubscriber\\MainContentViewSubscriber->onViewRenderArray()\n#31 /var/www/html/test/drupal9/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func()\n#32 /var/www/html/test/drupal9/vendor/symfony/http-kernel/HttpKernel.php(174): Drupal\\Component\\EventDispatcher\\ContainerAwareEventDispatcher->dispatch()\n#33 /var/www/html/test/drupal9/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw()\n#34 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\\Component\\HttpKernel\\HttpKernel->handle()\n#35 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\\Core\\StackMiddleware\\Session->handle()\n#36 /var/www/html/test/drupal9/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\\Core\\StackMiddleware\\KernelPreHandle->handle()\n#37 /var/www/html/test/drupal9/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\\page_cache\\StackMiddleware\\PageCache->pass()\n#38 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\\page_cache\\StackMiddleware\\PageCache->handle()\n#39 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle()\n#40 /var/www/html/test/drupal9/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\\Core\\StackMiddleware\\NegotiationMiddleware->handle()\n#41 /var/www/html/test/drupal9/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\\StackedHttpKernel->handle()\n#42 /var/www/html/test/drupal9/web/index.php(19): Drupal\\Core\\DrupalKernel->handle()\n#43 {main}, referer: http://localhost/test/drupal9/web/fr/node/add/page

Proposed resolution

Have a check for links for languages
If Languages is not empty
and if Links is not empty too
and if Links is an array
then use the array_keys

Changes in the Drupal core Language Block
$links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRouteMatch(\Drupal::routeMatch()));

https://git.drupalcode.org/project/drupal/-/commit/18bce0cd518975407921b...

Remaining tasks

  • File an issue
  • Patch/MR
  • Test
  • Review
  • Release

User interface changes

  • N/A

API changes

  • N/A

Data model changes

  • N/A
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

Rajab Natshah created an issue. See original summary.

rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

StatusFileSize
new2.03 KB

rajab natshah’s picture

Assigned: rajab natshah » Unassigned
Status: Active » Needs review
rajab natshah’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 4: 3348277-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rajab natshah’s picture

Having issues with
Hide if only one language is available

If only a single language is available, go ahead and hide the block

Not having the same old expected behavior.

rajab natshah’s picture

Maybe related to changes after the fix for
Drupal core - Moderately critical - Information disclosure - SA-CORE-2023-003

The language module provides a Language switcher block which can be placed to provide links to quickly switch between different languages.
The URL of unpublished translations may be disclosed. When used in conjunction with a module like Pathauto, this may reveal the title of unpublished content.

The block will not show up in many pages.

rajab natshah’s picture

In Drupal core Language Block
$links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRouteMatch(\Drupal::routeMatch()));

https://git.drupalcode.org/project/drupal/-/commit/18bce0cd518975407921b...

rajab natshah’s picture

StatusFileSize
new3.66 KB

This patch for the 8.x-2.x-dev branch

rajab natshah’s picture

Status: Needs work » Needs review
rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

rajab natshah’s picture

StatusFileSize
new3.73 KB

Attached a patch fix for the 8.x-2.0 released tag
It will fail to apply to the DEV branch. ( only use for the tag release )
The patch from #12 will apply to 2.x-dev

nsciacca’s picture

+1 for the patch in #16, thanks!

prashant.c’s picture

Status: Needs review » Needs work

Patch fails to apply on Drupal 9.5 and module version 8.x-2.x

error: patch failed: src/Plugin/Block/LanguageDropdownBlock.php:714

prashant.c’s picture

I personally experienced this error with only logged-in users. The root cause is the change record mentioned at https://www.drupal.org/node/3247792. To resolve this issue, one possible solution is to cast the <?php array_keys((array)$languages->links) ?> to an array.

rajab natshah’s picture

Status: Needs work » Needs review
  • Patch #12 is for the development branch 8.x-2.x
  • Patch #16 is for the latest release tag 8.x-2.0

  • Rajab Natshah committed de9c1d1d on 8.x-2.x
    Issue #3348277: Fix TypeError: array_keys(): Argument #1 ($array) must...
rajab natshah’s picture

Status: Needs review » Fixed
rajab natshah’s picture

menu2crak’s picture

StatusFileSize
new86.73 KB

Hello, after applying patch 16, I solved the error "TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in Language Drop down Block class" the problem that now gives me another error new. The error occurs because you are trying to count a null variable. Line 239 web/modules/contrib/dropdown_language/src/Plugin/Block/DropdownLanguage.php

Do I have to make a new patch?

Greetings and thank you

bramvandenbulcke’s picture

Thanks for the quick fix Rajab!

prashant.c’s picture

@menu2crak

No, you do not have to make a new patch. The patch is already there and committed. Regarding contributing to Drupal you can go through the documents

https://www.drupal.org/community/contributor-guide/contribution-areas
https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...

Status: Fixed » Closed (fixed)

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

londova’s picture

Is there any plan to issue a new module version with this patch included?

laurent222’s picture

Thanks Rajab for your patch

carma03’s picture

Confirming patch #16 worked for 8.x-2.0 on Drupal 9.5.8 and PHP 8.1.
Thanks @rajab-natshah !

CedricCrettaz’s picture

When do you expect to release a new module version correcting this error?

chkavak’s picture

Hello.
Thanks for your answer @Rajab Natshah. I had this problem in Drupal version 9.5.5 and your solution solved the problem.
Now I'm using Drupal version 9.5.9, after updating to version 9.5.8 and 9.5.9 I get the following error when selecting the language in the LanguageDropdown:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function all() on null in Drupal\lang_dropdown\Form\LanguageDropdownForm->submitForm() (line 347 of /var/www/html/drupal/modules/lang_dropdown/src/Form/LanguageDropdownForm.php)

Please help me, I can't find the solution on the internet

thhafner’s picture

Agreed with above posters. This issue isn't really fixed until there is an actual release of the module with patch #16 included.

rajab natshah’s picture

Issue tags: +lang_dropdown-8.x-2.1
rajab natshah’s picture