Problem/Motivation

Following error is thrown while upgrading from Drupal 8.7 to Drupal 9.4:

Deprecated function: addcslashes(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Core\Database\Connection->escapeLike() (line 1525 of core/lib/Drupal/Core/Database/Connection.php).

Drupal\Core\Database\Connection->escapeLike(NULL) (Line: 420)
Drupal\Core\Database\Query\Select->escapeLike(NULL) (Line: 118)
Drupal\Core\Entity\Query\Sql\Condition::translateCondition(Array, Object, ) (Line: 63)
Drupal\Core\Entity\Query\Sql\Condition->compile(Object) (Line: 176)
Drupal\Core\Entity\Query\Sql\Query->compile() (Line: 80)
Drupal\Core\Entity\Query\Sql\Query->execute() (Line: 81)
Drupal\dspace_harvester\Controller\HarvesterController->harvest() (Line: 31)
dspace_harvester_cron(Object)
call_user_func('dspace_harvester_cron', Object) (Line: 325)
Drupal\ultimate_cron\Entity\CronJob->invokeCallback() (Line: 471)
Drupal\ultimate_cron\Entity\CronJob->run(Object) (Line: 24)
Drupal\ultimate_cron\Controller\JobController->runCronJob(Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
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: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 49)
Asm89\Stack\Cors->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: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 21)

Steps to reproduce

Proposed resolution

I have already tried the steps mentioned in the below url but, still the error occurs:

https://www.drupal.org/project/paragraphs/issues/3253545

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#12 database-escapeLike.patch483 bytestahirpatel

Comments

tushar.k created an issue. See original summary.

cilefen’s picture

I would look into the contributed or custom modules shown in the stack trace.

cilefen’s picture

dspace_harvester seems the likely root cause.

longwave’s picture

Title: Drupal 9.4 and PHP 8.1.2 » Deprecated function: addcslashes(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Core\Database\Connection->escapeLike()

Better title, but I also think this is a custom code issue; looks like your HarvesterController::harvest() method is passing NULL where a string is expected.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
tushar.k’s picture

@cilefen and @longwave thankyou
I'll recheck the code once again and get back to you.

jfuentes’s picture

This issue happens to me adding a custom block (of core).... Just after upgrade from 9.4.5 to 9.4.8 and PHP 8.1

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.

cilefen’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
aaronelborg’s picture

I think I fixed this by updating to the latest and greatest Paragraphs module. Does that sound familiar to anyone else?

paintingguy’s picture

Deprecated function: addcslashes(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Core\Database\Connection->escapeLike() (line 1525 of core/lib/Drupal/Core/Database/Connection.php

I’m getting this when creating new registrations.

Php 8.1 Drupal core 9.5.3

tahirpatel’s picture

StatusFileSize
new483 bytes

It's not recommended to modify core files in Drupal. This can cause issues with future updates and maintenance of the site. Instead, you should create a patch file and add it to composer.

here are the steps to create a patch file and add it to composer:

Create a copy of the file Connection.php in the directory core/lib/Drupal/Core/Database to preserve the original file.

Edit the Connection.php file and replace the following code:

public function escapeLike($string) {
  return addcslashes($string, '\%_');
}

With:

public function escapeLike($string) {
  if (!isset($string)) {
    return $string;
  }
  return addcslashes($string, '\%_');
}

Navigate to the root directory of your Drupal installation in the terminal/command prompt.

Run the following command to create a patch file:

git diff > path/to/your/patch/file.patch

Replace path/to/your/patch/file.patch with the path where you want to save the patch file.

Open the composer.json file located in the root directory of your Drupal installation.

Add the following code to the extra section of the composer.json file:

"patches": {
  "drupal/core": {
    "EscapeLike function": "path/to/your/patch/file.patch"
  }
}

Replace EscapeLike function with a short description of the patch, and path/to/your/patch/file.patch with the actual path where you saved the patch file.

Save the composer.json file and run the following command to apply the patch:

composer install

This will download the latest version of Drupal core and apply the patch automatically. If you have already installed Drupal core, you can run composer update instead of composer install.

That's it! The escapeLike() function will now return the original string if it is not set, and the patch will be applied automatically whenever you update or install Drupal core using composer.

=============
if the patch not applies, you need to install composer 'cweagans/composer-patches'.

The cweagans/composer-patches package is a Composer plugin that allows you to apply patches to third-party packages that you've installed via Composer.

To use this plugin, you can follow these steps:

Install the cweagans/composer-patches package via Composer:
composer require cweagans/composer-patches

Add the patch to your composer.json file:

{
    "extra": {
        "patches": {
            "drupal/core": {
                "EscapeLike function fix": "patches/database-escapeLike.patch"
            }
        }
    }
}

Run composer install or composer update drupal/core to apply the patch.

tahirpatel’s picture

if patch not working you need to install 'cweagans/composer-patches'

The cweagans/composer-patches package is a Composer plugin that allows you to apply patches to third-party packages that you've installed via Composer.

To use this plugin, you can follow these steps:

Install the cweagans/composer-patches package via Composer:
composer require cweagans/composer-patches

Add the patch to your composer.json file:

{
    "extra": {
        "patches": {
            "drupal/core": {
                "EscapeLike function fix": "patches/database-escapeLike.patch"
            }
        }
    }
}

Run 'composer install' or 'composer update drupal/core' to apply the patch.