header array empty in theme.inc TypeError: strlen(): Argument must be of type string, array given in Unicode.php

Created on 19 December 2023, 10 months ago
Updated 30 April 2024, 6 months ago

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.

Merge request link

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

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Theme 

Last updated about 10 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @aduthois
  • Status changed to Postponed: needs info 10 months ago
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Are you able to reproduce this with vanilla Drupal core?

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

    Thanks

  • 🇬🇧United Kingdom longwave UK

    The extra [] was added in 📌 Allow for deletion of a single value of a multiple value field Fixed but not sure how to reproduce the issue.

  • 🇵🇭Philippines danielhonrade

    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.

  • Are you using Bootsrap theme? May be a duplicate of 🐛 PHP error when form has entity reference in Drupal 10.2.0 RTBC .

  • 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 ?

  • Status changed to Active 9 months ago
  • @larowlan - steps to reproduce using Bootstrap theme are described in 🐛 PHP error when form has entity reference in Drupal 10.2.0 RTBC , 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?

  • Status changed to Postponed: needs info 9 months ago
  • 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

  • Status changed to Needs review 9 months ago
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    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.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    25,852 pass, 1,780 fail
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    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.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    Build Successful
  • Status changed to Needs work 9 months ago
  • 🇺🇸United States smustgrave

    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.

  • Merge request !6323Resolve #3409752 "Header array empty" → (Open) created by Liam Morland
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    I have created a merge request with the patches.

  • Pipeline finished with Success
    9 months ago
    #82380
  • Hi! Comment #10: drupal-header_array_empty-3409752-10.patch works for me. Thanks!

  • 🇫🇷France hdnag

    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)

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    @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.

  • @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.

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    It should have a test.

  • First commit to issue fork.
  • Pipeline finished with Success
    6 months ago
    Total: 699s
    #160388
Production build 0.71.5 2024