The Drupal system I have inherited is throwing a large number of PHP warnings into the logs that look like this:

Warning: array_flip(): Can only flip STRING and INTEGER values in Drupal\Core\Entity\EntityStorageBase->loadMultiple()

I have spent some time running it through a debugger and reading existing tickets, this is what I have found:

There seem to be a number of circumstances that can lead to this error. Some error reports I have seen talk about it being caused by an ID value being turned into a float rather than an int, due to an earlier call to floor().

In my case the error occurs in the loadMultiple() method when an ID arrives wrapped in a Drupal\Core\Render\Markup object rather than being a plain integer or string.

In all cases I've seen, the value is correct, but is just not presented in a format that array_flip can handle.

It therefore seems to me that an appropriate fix would be to add a little pre-processing to normalise the array to contain only strings or integers.

I therefore propose that we add the following code to the loadMultiple() method, before the call to array_flip():

    if ($ids) {
      foreach ($ids as $key=>$value) {
        if (!is_string($value) && !is_integer($value)) {
          $ids[$key] = (string)$value;
        }
      }
    }

Comments

Spudley created an issue. See original summary.

spudley’s picture

Issue summary: View changes
raunak.singh’s picture

Assigned: Unassigned » raunak.singh
raunak.singh’s picture

Assigned: raunak.singh » Unassigned

Hi @Spudley,

I am not able to reproduce this issue.

lendude’s picture

Version: 8.8.x-dev » 9.0.x-dev
Component: views.module » entity system
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs steps to reproduce

Moving this out of the views queue since this does not seem to be related to views.

We would need some steps to reproduce the problem here. That way we can asses if adding a wrapper for sanitisation is the correct way of addressing this. Most of the time, these wrappers just hide the upstream problem. If invalid data is passed, the upstream code should be cleaned up and it should not be the responsibility of the called code to clean up everything it gets passed.

longwave’s picture

Indeed, the original post notes this:

> In my case the error occurs in the loadMultiple() method when an ID arrives wrapped in a Drupal\Core\Render\Markup object rather than being a plain integer or string.

Why is this method being called with a markup object in the first place? It should be the caller's responsibility to cast markup to string/integer. A stack trace may help figure out the root cause, and then it can be fixed there.

spudley’s picture

Version: 9.0.x-dev » 8.8.x-dev
Component: entity system » views.module
Status: Postponed (maintainer needs more info) » Active

@Lendude: Apologies for putting it in Views if that wasn't right. As you'll see below, it seems to me that the Views module is where the problem originates, even if the warning is eventually thrown in the core entity code.

At the very least, the error seems to be entirely within Drupal: there is no custom code involved here; even the templates it goes through in the trace below are ones that are built-in.

@longwave, you asked for a stacktrace, so here it is:

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 266 of /var/www/public/core/lib/Drupal/Core/Entity/EntityStorageBase.php)
#0 /var/www/public/core/includes/bootstrap.inc(587): _drupal_error_handler_real(2, 'array_flip(): C...', '/var/www/p...', 266, Array)
#1 [internal function]: _drupal_error_handler(2, 'array_flip(): C...', '/var/www/p...', 266, Array)
#2 /var/www/public/core/lib/Drupal/Core/Entity/EntityStorageBase.php(266): array_flip(Array)
#3 /var/www/public/core/lib/Drupal/Core/Entity/EntityBase.php(541): Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array)
#4 /var/www/public/core/modules/taxonomy/src/Plugin/views/argument/IndexTid.php(19): Drupal\Core\Entity\EntityBase::loadMultiple(Array)
#5 /var/www/public/core/modules/views/src/Plugin/views/argument/ManyToOne.php(153): Drupal\taxonomy\Plugin\views\argument\IndexTid->titleQuery()
#6 /var/www/public/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php(980): Drupal\views\Plugin\views\argument\ManyToOne->title()
#7 /var/www/public/core/modules/views/src/ViewExecutable.php(1110): Drupal\views\Plugin\views\argument\ArgumentPluginBase->getTitle()
#8 /var/www/public/core/modules/views/src/ViewExecutable.php(1267): Drupal\views\ViewExecutable->_buildArguments()
#9 /var/www/public/core/modules/views/src/ViewExecutable.php(1391): Drupal\views\ViewExecutable->build(NULL)
#10 /var/www/public/modules/contrib/views_field_view/src/Plugin/views/field/View.php(263): Drupal\views\ViewExecutable->execute()
#11 /var/www/public/core/modules/views/src/Plugin/views/field/FieldPluginBase.php(1155): Drupal\views_field_view\Plugin\views\field\View->render(Object(Drupal\views\ResultRow))
#12 /var/www/public/core/modules/views/views.theme.inc(236): Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object(Drupal\views\ResultRow))
#13 /var/www/public/core/lib/Drupal/Core/Theme/ThemeManager.php(287): template_preprocess_views_view_field(Array, 'views_view_fiel...', Array)
#14 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(437): Drupal\Core\Theme\ThemeManager->render('views_view_fiel...', Array)
#15 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#16 /var/www/public/core/modules/views/src/Plugin/views/field/FieldPluginBase.php(1747): Drupal\Core\Render\Renderer->render(Array)
#17 /var/www/public/core/modules/views/src/Plugin/views/style/StylePluginBase.php(761): Drupal\views\Plugin\views\field\FieldPluginBase->theme(Object(Drupal\views\ResultRow))
#18 [internal function]: Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow(Array)
#19 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(378): call_user_func(Array, Array)
#20 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#21 /var/www/public/core/modules/views/src/Plugin/views/style/StylePluginBase.php(709): Drupal\Core\Render\Renderer->render(Array)
#22 /var/www/public/core/modules/views/src/Plugin/views/style/StylePluginBase.php(576): Drupal\views\Plugin\views\style\StylePluginBase->renderFields(Array)
#23 /var/www/public/core/modules/views/src/Plugin/views/style/StylePluginBase.php(468): Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping(Array, Array, true)
#24 /var/www/public/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php(2111): Drupal\views\Plugin\views\style\StylePluginBase->render(Array)
#25 /var/www/public/core/modules/views/src/ViewExecutable.php(1533): Drupal\views\Plugin\views\display\DisplayPluginBase->render()
#26 /var/www/public/core/modules/views/src/Plugin/views/display/Block.php(131): Drupal\views\ViewExecutable->render()
#27 /var/www/public/core/modules/views/src/ViewExecutable.php(1630): Drupal\views\Plugin\views\display\Block->execute()
#28 /var/www/public/core/modules/views/src/Element/View.php(77): Drupal\views\ViewExecutable->executeDisplay('block_1', Array)
#29 /var/www/public/core/modules/views/src/Plugin/Block/ViewsBlock.php(59): Drupal\views\Element\View::preRenderViewElement(Array)
#30 /var/www/public/core/modules/block/src/BlockViewBuilder.php(163): Drupal\views\Plugin\Block\ViewsBlock->build()
#31 [internal function]: Drupal\block\BlockViewBuilder::preRender(Array)
#32 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(378): call_user_func('Drupal\\block\\Bl...', Array)
#33 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(450): Drupal\Core\Render\Renderer->doRender(Array)
#34 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#35 /var/www/public/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#36 /var/www/public/sites/default/files/php/twig/5ed11c0daabf6_page.html.twig_T3-gWHGv2-QN5j1a5Yua4gorp/ZCBt8WmIXFjboVNuRRMtg-3PRX4i8NfX7XLFiBE80Pw.php(114): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#37 /var/www/vendor/twig/twig/src/Template.php(455): __TwigTemplate_8d55be4594adce225e5f5f6d8ab6b3bb08bee932e081844868dec3907889b469->doDisplay(Array, Array)
#38 /var/www/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#39 /var/www/public/sites/default/files/php/twig/5ed11c0daabf6_page.html.twig_82qaosnAa0mPmQS46IWTHYv93/66_KzCrC5Lx4RFdhkfpO6r6bfkrKawNhGX1gnsGPxdc.php(63): Twig\Template->display(Array, Array)
#40 /var/www/vendor/twig/twig/src/Template.php(455): __TwigTemplate_d268c2421be85b545fb347874df91ac67db27fab5b29b088f409bd517880c6c2->doDisplay(Array, Array)
#41 /var/www/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#42 /var/www/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#43 /var/www/public/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#44 /var/www/public/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#45 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(437): Drupal\Core\Theme\ThemeManager->render('page', Array)
#46 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#47 /var/www/public/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#48 /var/www/public/sites/default/files/php/twig/5ed11c0daabf6_html.html.twig_lF7jLf14fYXsIlzWqHKz4ua93/xne_NOrO_h0LWKyaZj0KWL9CugA0rziIWjQgxMwhMcs.php(100): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#49 /var/www/vendor/twig/twig/src/Template.php(455): __TwigTemplate_01bf7fa57d13b4ebd85b8949ff072133df46dbd6ac3a91932a874d2e4f85db94->doDisplay(Array, Array)
#50 /var/www/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#51 /var/www/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#52 /var/www/public/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#53 /var/www/public/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/contrib/...', Array)
#54 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(437): Drupal\Core\Theme\ThemeManager->render('html', Array)
#55 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#56 /var/www/public/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(147): Drupal\Core\Render\Renderer->render(Array)
#57 /var/www/public/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#58 /var/www/public/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(148): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#59 /var/www/public/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#60 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#61 /var/www/public/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#62 /var/www/vendor/symfony/http-kernel/HttpKernel.php(156): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent))
#63 /var/www/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#64 /var/www/public/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#65 /var/www/public/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#66 /var/www/public/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#67 /var/www/public/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#68 /var/www/public/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#69 /var/www/public/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#70 /var/www/public/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#71 /var/www/public/modules/contrib/detect_mobile/src/Middleware/MobileDetectMiddleware.php(61): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#72 /var/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\detect_mobile\Middleware\MobileDetectMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#73 /var/www/public/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#74 /var/www/public/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#75 {main}
spudley’s picture

Version: 8.8.x-dev » 9.0.x-dev
Component: views.module » entity system
spudley’s picture

Apologies for flipping the version/component settings back; I hadn't refreshed the page, so when I saved the new comment, it picked up the original values. I've put them back to the way you set them @Lendude.

longwave’s picture

#44 /var/www/public/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)

There is a custom Twig template involved, what does this do? A Views block gets rendered inside it, it seems like the arguments to this block might not be the correct type.

spudley’s picture

I think I've resolved this problem. I've made a number of other changes to the site in question, and the errors described here have now disappeared.

The change that I think sorted it was where I removed an older third-party module and replaced it with a newer one that is properly supported and up-to-date. The error seems to have gone away after I did this.

The older module linked into the views and allowed them to alter their behaviour according to the user's browser. This feature wasn't being used, but it would seem that the module was getting called by Views anyway and passed the field output through markup renderer regardless of whether it was necessary or correct.

I replaced the module for other reasons unrelated to this bug, but it was only the act of removing the module that allowed me to realise that it was also causing this bug. I honestly don't think I would have managed to track the bug down otherwise; I certainly had no idea it was linked to the module. In fact, I didn't even know the module had functionality that linked into Views.

Tracking this down has been an absolute nightmare. The warning message occurs at a point in the Drupal code that is very *very* far removed from where the problem originates. I've seen this a lot before in Drupal. Usually, where I've made a mistake in my own code, it's possible to work out what's going on, but in cases like this it's just impossible without spending literally days trawling through the code in a debugger.

I think the lesson to be drawn here is that Drupal needs much more robust error handling to pick up issues like this much sooner. A broken data structure that will cause a crash should be picked up early and reported on, not left until it actually crashes.

It's all very well saying that it's the module's fault. Yes it is. But the platform needs to give us the tools to be able to actually find the problem so that we can fix the module.

Version: 9.0.x-dev » 9.1.x-dev

Drupal 9.0.10 was released on December 3, 2020 and is the final full bugfix release for the Drupal 9.0.x series. Drupal 9.0.x will not receive any further development aside from security fixes. Sites should update to Drupal 9.1.0 to continue receiving regular bugfixes.

Drupal-9-only bug reports should be targeted for the 9.1.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.2.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Version: 9.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rnaik16’s picture

StatusFileSize
new901 bytes
rnaik16’s picture

rnaik16’s picture

StatusFileSize
new901 bytes

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

qusai taha’s picture

Status: Active » Needs review

The patch working for me,
Thank you!

_utsavsharma’s picture

StatusFileSize
new801 bytes
new900 bytes

Fixed CCF for #18.

longwave’s picture

Status: Needs review » Closed (duplicate)

Closing as duplicate of #3221534: throw an exception when IDs passed to loadMultiple() are badly formed as noted in #13. As far as I know this only happens when the set of IDs passed to loadMultiple is malformed in some way (usually a NULL); the solution is to fix the caller, but we can provide a better error message as well.

pamelalies’s picture

StatusFileSize
new900 bytes

This patch caused my site to throw: Warning: Array to string conversion in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 315 of core/lib/Drupal/Core/Entity/EntityStorageBase.php). Just posting this patch to undo it.

janpongos’s picture

#18 works for me thanks!

arx-e’s picture

Same problem with Drupal 11.2.5.
A few days ago and for reasons I have not traced to any specific change, I started having this "Can only flip string and integer values" error and it prevented a view block from loading in the pages it was placed. The view relies on filtering for the current user.

Warning: array_flip(): Can only flip string and integer values, entry skipped in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 279 of /home/blueflag/public_html/new/drupalsite/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php)

Adding the lines of code from #21 after line 278 in core/lib/Drupal/Core/Entity/EntityStorageBase.php solved my issue. The view is loaded again as it is expected and no errors are coming up. (The position where the lines should be inserted is a bit different in the current file in Drupal 11.2.5)