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
- Install Drupal with Webform module on PHP 8.3
- Create a content type with a webform field
- Create a node with a webform attached
- Preview the node
- 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
- Create a patch for the Webform module
- Test the patch on PHP 8.1, 8.2, and 8.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.