PHP 8.1 deprecation error in str_replace

Created on 13 May 2025, 3 days ago

Problem/Motivation

On a site that has contact form categories, visiting a category URL without the subject in the URL causes an error on PHP 8.1.

Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in contact_forms_form_alter() (line 149 of modules/contact_forms/contact_forms.module

Steps to reproduce

Create a contact category called "Test".

Visit http://example.com/contact/Test

That leads to the error:
Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Proposed resolution

The problem is on line 149:
$subject = str_replace( $replacement , ' ' , arg(2));

In the example URL above, arg(2) is null because the URL only has the Category name, but not the Subject.

Resolution: Add a null coalescing operator and return an empty string.
$subject = str_replace( $replacement , ' ' , arg(2) ?? '');

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States hargobind Austin, Texas

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024