Problem/Motivation

With PHP8.1, on the status report page, I get that message :
Deprecated function : htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated dans Drupal\Component\Utility\Html::escape() (/project/web/core/lib/Drupal/Component/Utility/Html.php ligne 427)

Steps to reproduce

Enable the captcha module on a standard Drupal 9.4 release, on a PHP8.1 platform.
Clear the cache and reload the status report page (/admin/reports/status)
You should see the error.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork captcha-3307927

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

pguillard created an issue. See original summary.

pguillard’s picture

Status: Active » Needs review
StatusFileSize
new643 bytes
pguillard’s picture

StatusFileSize
new641 bytes
pguillard’s picture

Issue summary: View changes
balintpekker’s picture

Tested locally on Drupal 9.4.0 and Drupal 9.4.5 (the latest release), went through the steps to reproduce, and couldn't find any issue. Also checked the message in the issue description which implies that \Drupal\Component\Utility\Html::escape($text) should be called somewhere in the CAPTCHA module with an empty $text but I couldn't find any representation of such code, not even anything calling the escape() method.

@pguillard Are you sure this is the module throwing the error message and not something else? Could you provide a little bit more information on the exact Drupal core version where this problem occurs?

Version information:
- CAPTCHA 1.4.0
- Drupal: 9.4.5
- PHP: 8.1.8
- MySQL: 8.0.23
- Server: nginx/1.19.7

balintpekker’s picture

StatusFileSize
new282.08 KB

Also adding a full page screenshot of the status report page where the specified message is not present.

pandaski’s picture

StatusFileSize
new158.26 KB

Highly it is caused by this issue here https://www.drupal.org/project/captcha/issues/3293710

The steps to reproduce from the issue:

- Install PHP 8.1
- Install Captcha and Database Logging modules
- Enable both Captcha and Database Logging modules
- Navigate to admin/config/people/captcha/captcha-points
- Enable any existing captcha point without editing
- Navigate to admin/reports/dblog?type%5B%5D=php&severity%5B%5D=7
- A deprecated function message will be found.

What happened here is:

CAPTCHA point form ID is set to null by default and gives a deprecated function message in logs when enabled.

Inside the module, it has an addMessage function trying to read the label

'%label' => $this->entity->label()

but it is null by default - https://git.drupalcode.org/project/captcha/-/blob/8.x-1.x/config/install...

Ref: https://git.drupalcode.org/project/captcha/-/blob/8.x-1.x/src/Form/Captc...

japerry’s picture

Status: Needs review » Closed (duplicate)
kaszarobert’s picture

Release 1.8 still produces for me this when using PHP 8.1. Initializing the variable to 0 manually fixed the problem:
drush php:eval "\Drupal::state()->set('captcha.wrong_response_counter', 0);"

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

hswong3i’s picture

Title: php8.1 Deprecated function : htmlspecialchars(): Passing null to parameter #1 » [D11] php8.1 Deprecated function : htmlspecialchars(): Passing null to parameter #1
Version: 8.x-1.4 » 2.x-dev
Status: Closed (duplicate) » Needs review

Similar as #9 @kazarobert reported, now when running with PHP 8.3 + Drupal 11.0.4 + Captcha 2.0.x-dev, with following condition:

  1. Newly installed Drupal
  2. NONE of wrong response captcha, yet (i.e. captcha.wrong_response_counter now return null)
  3. Enable "Enable statistics"
  4. Access Status Report page (i.e. trigger captcha_requirements() from captcha.install)

Will generate following PHP error message:

[Sun Sep 29 10:47:36 2024] Uncaught PHP Exception TypeError: "Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given, called in /home/hswong3i/Documents/drustack/composer-project-website-skeleton-11.0.x/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 256" at /home/hswong3i/Documents/drustack/composer-project-website-skeleton-11.0.x/core/lib/Drupal/Component/Utility/Html.php line 431

Simply patch as below could fix the problem:

diff --git a/captcha.install b/captcha.install
index f8161f6..c68361a 100755
--- a/captcha.install
+++ b/captcha.install
@@ -97,7 +97,7 @@ function captcha_requirements($phase) {
     $requirements['captcha_wrong_response_counter'] = [
       'title' => \Drupal::translation()->translate('CAPTCHA'),
       'value' => \Drupal::translation()->formatPlural(
-        \Drupal::state()->get('captcha.wrong_response_counter'),
+        \Drupal::state()->get('captcha.wrong_response_counter', 0),
         'Already 1 blocked form submission',
         'Already @count blocked form submissions'
       ),

hswong3i changed the visibility of the branch 3307927-php8.1-deprecated-function to hidden.

hswong3i changed the visibility of the branch 3307927-php8.1-deprecated-function to active.

ram4nd’s picture

Status: Needs review » Reviewed & tested by the community

Tested that the patch works and solves the error on "Status report" page.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Good catch hswong3i, committed!

  • japerry committed 5570b0b4 on 2.x authored by hswong3i
    Issue #3307927 by hswong3i: [D11] php8.1 Deprecated function :...
japerry’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.