Avoid addcslashes() deprecation warning during Webform submission preview

Created on 12 May 2025, 23 days ago

Problem/Motivation

In PHP 8.3, a deprecation warning is triggered when the addcslashes() function receives NULL instead of a string for its first parameter. This warning occurs in the Webform module when previewing nodes containing webforms. Specifically, the issue happens in WebformSubmissionForm::displayMessages() when getStorage()->getTotal() is called during node preview and processes a database query that may contain null values, leading to:

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

Steps to reproduce

  1. Install Drupal with Webform module on PHP 8.3
  2. Create a content type with a webform field
  3. Create a node with a webform attached
  4. Preview the node
  5. Check PHP error logs to see the deprecation warning

Proposed resolution

Modify the conditional statement in WebformSubmissionForm::displayMessages() that checks for previous submissions to avoid executing database queries with potentially null values during node preview operations:

// Display link to previous submissions message when user is adding a new submission.
if (
  $this->isGet()
  && $this->operation === 'add'
  && !str_ends_with($this->getRouteMatch()->getRouteName(), '.preview')
  && $this->getWebformSetting('form_previous_submissions', FALSE)
  && ($webform->access('submission_view_own') || $this->currentUser()->hasPermission('view own webform submission'))
  // Add this line to check if the source entity is in preview mode
  && !($this->sourceEntity && $this->sourceEntity->hasField('in_preview') && !empty($this->sourceEntity->in_preview))
  && ($previous_submission_total = $this->getStorage()->getTotal($webform, $this->sourceEntity, $this->currentUser()))
) {
  // ...remaining code...
}

This prevents the code from attempting to calculate submission totals when a node is in preview mode, avoiding the database queries that trigger the deprecation warning.

Remaining tasks

  1. Create a patch for the Webform module
  2. Test the patch on PHP 8.1, 8.2, and 8.3
  3. Review and commit the patch

User interface changes

None. The fix only prevents deprecation warnings without changing any user-facing functionality.

API changes

None. This is a defensive coding fix to avoid passing null values to addcslashes().

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

6.3

Component

Code

Created by

🇪🇸Spain marthinal

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024