Problem/Motivation

TypeError: strlen(): Argument #1 ($string) must be of type string, array given in strlen() (line 395 of core/lib/Drupal/Component/Utility/Unicode.php)

The problem is in template_preprocess_field_multiple_value_form() where there is an empty array in the header.

$header = [
      [
        'data' => [
          '#type' => 'html_tag',
          '#tag' => 'h4',
          '#value' => $element['#title'],
          '#attributes' => $header_attributes,
        ],
        'colspan' => 2,
        'class' => ['field-label'],
      ],
      [], // Problem is here.
      t('Order', [], ['context' => 'Sort order']),
    ];

Steps to reproduce

To be added

Proposed resolution

Remove the cell and increase the colspan of the previous cell.

https://git.drupalcode.org/project/drupal/-/merge_requests/6323

Remaining tasks

Add test coverage

User interface changes

Removal of empty header cell.

API changes

None.

Data model changes

None.

Release notes snippet

TBD

Issue fork drupal-3409752

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

aduthois created an issue. See original summary.

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Are you able to reproduce this with vanilla Drupal core?

Can you please provide steps to reproduce starting f from install Drupal?

Thanks

longwave’s picture

danielhonrade’s picture

I am having the same problem, I don't know why because a vanilla Drupal install didn't get that error, but adding an override of that function and making [] to ' ' fixed it.

gaddman’s picture

Are you using Bootsrap theme? May be a duplicate of #3409370: PHP error when form has entity reference in Drupal 10.2.0.

aduthois’s picture

Yes, Patch Issues-3409370-PHP-error-when-form-has-entity-reference.patch
solve the problem but for me is a core problem not bootstrap theme problem :
function template_preprocess_field_multiple_value_form add a empty array, why ?

gaddman’s picture

gaddman’s picture

Status: Postponed (maintainer needs more info) » Active

@larowlan - steps to reproduce using Bootstrap theme are described in #3409370: PHP error when form has entity reference in Drupal 10.2.0, reproduced here:

  1. Drupal 10.2.0 standard install profile with bootstrap 3.29.
  2. Ensure bootstrap theme active for creating content - either login as a non-admin user or disable Use the administration theme when editing or creating content at /admin/appearance.
  3. Edit the article structure to add a new field:
    1. Label = Users
    2. Field type = Reference
    3. Reference type = User
    4. Rest as default
  4. Add article content (/node/add/article) - page loads OK.
  5. Go back to the Users field settings and set the Allowed number of values to a value > 1 or Unlimited.
  6. Add article content (/node/add/article) - page fails to load and PHP error is observed in log.

To @aduthois' point - is this considered a core issue or should the fix be in Bootstrap?

aduthois’s picture

Status: Active » Postponed (maintainer needs more info)

I suppose if drupal core has a good reason to add an empty array, boostrap should adapt to the case. So ok for this patch

liam morland’s picture

Version: 10.2.x-dev » 11.x-dev
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new422 bytes

I am having this problem.

The documentation for template_preprocess_table() says that the array values in the #header property need to be either a string or an array with certain keys. An empty array is not consistent with the documentation. Correcting it makes sense for this reason. The attached patch does this and fixes the issue for me.

liam morland’s picture

StatusFileSize
new540 bytes

There is an underlying issue here as well: As it is, there is an empty th element created. That shouldn't be. One option would be to give the column a title, perhaps "Actions". Another would be to remove the cell and increase the colspan of the previous one; see this patch for implementation.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs issue summary update

Recommend using an MR, as DrupalCI is less supported and may not be as accurate in my opinion.

But currently the patch is showing failures.

Also will need test coverage

Issue summary should be updated with standard template. Added the template but will have to be updated.

liam morland’s picture

Issue summary: View changes

I have created a merge request with the patches.

Tests in the merge request are passing.

Issue template updated.

clota01’s picture

Hi! Comment #10: drupal-header_array_empty-3409752-10.patch works for me. Thanks!

hdnag’s picture

Hello,

I have the same issue but for D9.5.11 :
The patches are non applicables. Do you have an idea to solve that issue ?

TypeError: strlen(): Argument #1 ($string) must be of type string, array given in strlen() (line 477 of core/lib/Drupal/Component/Utility/Unicode.php).
strlen(Array) (Line: 477)
Drupal\Component\Utility\Unicode::validateUtf8(Array) (Line: 65)
Drupal\Component\Utility\Xss::filter(Array, Array) (Line: 753)
Drupal\Core\Render\Renderer->ensureMarkupIsSafe(Array) (Line: 380)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 50)
__TwigTemplate_2521555a241b9ba0b3f8cdba9a2b0ebc->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/form-element-label.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('form_element_label', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 60)
__TwigTemplate_661ab2f707079e55824bbaecf556a958->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/form-element.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('form_element', Array) (Line: 489)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 89)
__TwigTemplate_683f4aab1c1850ba7b0b2ff00cc5518b->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/form/field-multiple-value-form.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('field_multiple_value_form', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 179)
__TwigTemplate_5bcd06882cab0ed5e3210b27209930bc->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/classy/dataset/table.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('table', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 56)
__TwigTemplate_683f4aab1c1850ba7b0b2ff00cc5518b->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/form/field-multiple-value-form.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('field_multiple_value_form', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 44)
__TwigTemplate_a452e371f89bc4dde9344d99b72c7065->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/node-edit-form.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('node_edit_form', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 130)
Drupal\cdn\StackMiddleware\DuplicateContentPreventionMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

liam morland’s picture

@hdnag your issue must be caused by something else. This issue is about a bug introduced in Drupal 10.2. Drupal 9 in no longer supported.

m_hobby’s picture

@Liam Morland, thanks #10 in my case resolves the bug.
@smustgrave, is there anything else (test coverage?) that is needed to get this issue across the line?
Many thanks.

liam morland’s picture

It should have a test.

naveenvalecha made their first commit to this issue’s fork.

fathima.s’s picture

Same error occurs with bootsrap5 theme.Above patches didnt work for me .Is there any other way to fix in bootstrap5

"TypeError: strlen(): Argument #1 ($string) must be of type string, array given in strlen() (line 395 of core/lib/Drupal/Component/Utility/Unicode.php)."

kopeboy’s picture

Having the same error at /admin/config/services/openapi

shweta__sharma’s picture

Title: header array empty in theme.inc TypeError: strlen(): Argument must be of type string, array given in Unicode.php » Header array empty in theme.inc TypeError: strlen(): Argument must be of type string, array given in Unicode.php
Issue tags: +Needs steps to reproduce

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.