Fix TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in Language Drop down Block class

Created on 15 March 2023, almost 2 years ago
Updated 29 June 2023, over 1 year ago

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
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇯🇴Jordan Rajab Natshah Jordan

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

Comments & Activities

Production build 0.71.5 2024