TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given

Created on 8 May 2025, 3 months ago

TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given, called in web\core\lib\Drupal\Component\Render\FormattableMarkup.php on line 238 in Drupal\Component\Utility\Html::escape() (line 431 of web\core\lib\Drupal\Component\Utility\Html.php).

upgrade to drupal 11 create logger and pass null value like this or whenever you use variable driven from field and this filed is empty
\Drupal::logger("test")->info("pass null @var",["@var"=>null])

go to /admin/reports/dblog the error above will appear

๐Ÿ› Bug report
Status

Needs review

Version

11.0 ๐Ÿ”ฅ

Component

render system

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @Amer cloudy
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ishani Patel

    ishani patel โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ishani Patel

    I've created MR from the patch.

    Thank you!

  • Pipeline finished with Failed
    3 months ago
    Total: 243s
    #492125
  • Pipeline finished with Failed
    3 months ago
    Total: 711s
    #492129
  • Pipeline finished with Success
    3 months ago
    Total: 542s
    #492153
  • Alternatively you have the option of changing your code not to pass a null.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Issue summary is incomplete.

    But also need some more investigation, what's being passed here that's null. If caused by custom code then seems the custom code is wrong.

  • In fact, in these cases the log messages would make no sense without values in placeholders.

  • Yes the issue related to custom code and i already have production that save logs in database with null it works with D10 and when upgrade to drupal 11 the issue appears

  • ๐Ÿ‡ฌ๐Ÿ‡ทGreece arx-e

    I had the same error trying to access a simplenews page (admin/config/services/simplenews/settings/subscription) and this patch solves the issue.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly uccio Turin

    I had the same error trying to access to admin/reports/status in a site with a lot of module contrib but only after the upgrade drupal 10.4.7
    -> 11.1.7

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States _andrew Pennsylvania

    Please see supplied patch which should apply cleanly in 11.1 and probably 11.2 which solves the issue. Relaxes the string requirement allowing for a null argument, and returns an empty string on null.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mighty_webber

    11.2.2 made some changes that made patch #14 not work correctly. These are the same changes in patch 14, just applied to different line numbers.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States ryan osฤซtis

    I was attempting to test the patch from #14 ๐Ÿ› TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given Needs review , but patching is affected as well. Insidious little bug, this one.

    • PHP: 8.3
    • Drupal: 11.2.2
    • Original Triggering Action: I attempted to enable the Gin Admin Theme as the default theme on a clean new Drupal 11 site. I then tried to install the patch as a fix for the null text error I recieved. All pages on the site now display it.
    • Error Message: While using composer install, this was returned:
    Gathering patches from patch file.
      Gathering patches for dependencies. This might take a minute.
        - Installing drupal/core (11.2.2): Extracting archive
        - Applying patches for drupal/core
          fix-type-error-null-text.patch (Fix type error null text)
      
      In RemoteFilesystem.php line 105:
                                                                                                                           
        [TypeError]                                                                                                        
        Composer\Util\RemoteFilesystem::copy(): Argument #1 ($originUrl) must be of type string, null given, called in /v  
        ar/www/html/vendor/cweagans/composer-patches/src/Patches.php on line 388                                           
                    
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mighty_webber

    I've attempted to apply it using extra.patches.drupal/commerce, extra.patches.drupal/core, extra.patches.drupal/core-recommended, all of which fail.

    patch "-p4" --no-backup-if-mismatch -d "M:\development\projects\react-safe-drupal/web/modules/contrib/commerce" < "C:\Users\User\AppData\Local\Temp/686448b16b77a.patch"
    can't find file to patch at input line 5
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |diff --git a/web/core/lib/Drupal/Component/Utility/Html.php b/web/core/lib/Drupal/Component/Utility/Html.php
    |index 1a53a8de..e4426fe4 100644
    |--- a/web/core/lib/Drupal/Component/Utility/Html.php
    |+++ b/web/core/lib/Drupal/Component/Utility/Html.php
    --------------------------
    File to patch:
    Skip this patch? [y]
    Skipping patch.
    2 out of 2 hunks ignored
    
       Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2025-07-01/fix-type-error-null-text-3523317-15.patch

    I have attempted with both patches.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mighty_webber

    Ok, I think I resolved the issue with it. Specifically, the file pointers.

    When composer is installing, it's installing from web/core. The patch points to web/core/lib.

    It should look like :

    diff --git a/lib/Drupal/Component/Utility/Html.php b/lib/Drupal/Component/Utility/Html.php
    index 1a53a8de..e4426fe4 100644
    --- a/lib/Drupal/Component/Utility/Html.php
    +++ b/lib/Drupal/Component/Utility/Html.php

    Instead of

    diff --git a/web/core/lib/Drupal/Component/Utility/Html.php b/web/core/lib/Drupal/Component/Utility/Html.php
    index 1a53a8de..e4426fe4 100644
    --- a/web/core/lib/Drupal/Component/Utility/Html.php
    +++ b/web/core/lib/Drupal/Component/Utility/Html.php
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mighty_webber

    As an added note, I think this patch creates issues with fields with "Unlimited" cardinality. When trying to root out the issue (couldn't create new fields with unlimited cardinality or save forms with existing fields with unlimited cardinality and "add another item" would fail, as the index values for the weight/order were coming in as null/empty strings.

    Oddly enough, after removing this patch from my installation, I didn't not continue receiving the error noted in this ticket when attempting to install commerce, and the functionality of fields with unlimited cardinality returned.

  • Status changed to Needs work 25 days ago
  • I manually applied the changes in patch at #15, it seems to have worked.

  • ๐Ÿ‡ท๐Ÿ‡บRussia Alezu

    The problem with the patch from #15 is that it considers zero symbol as empty that breaks sorting of multiple field elements and I believe not only it. So we should check if the input text is 0.

  • ๐Ÿ‡ท๐Ÿ‡บRussia Alezu
  • Changes in patch #15 seems to have caused problems with the weight variable throughout the site. Reverted this patch, which seems to have resolved the weight problem, then manually applied patch #21. Seems to work.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Summary update and steps to reproduce still appear to be missing

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States w01f

    Can confirm manually applying #21 resolves the issue on our test site.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom c_archer Cumbria

    Can confirm patch in #21 works.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    #24 is still needed and this needs to be reviewed

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
Production build 0.71.5 2024