Deprecated function: str_replace(): Passing null

Created on 31 August 2025, about 1 month ago

Problem/Motivation

When rendering ads with the ad_content module, a PHP deprecation notice is triggered on PHP 8.1+:

Deprecated function: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated
in Drupal\ad_content\Plugin\Ad\Bucket\AdContentBucket::postAdRender()
(line 252 of /modules/contrib/ad/modules/ad_content/src/Plugin/Ad/Bucket/AdContentBucket.php)

This happens when $build['#ad_impression_id'] is missing or null, which results in str_replace() receiving an invalid argument.

Steps to reproduce

  1. Install and enable the ad and ad_content modules.
  2. Create an ad bucket and render ads (for example via Mercury Editor or an ad placement).
  3. On PHP 8.1+ observe the deprecation warning in logs:
    str_replace(): Passing null to parameter #2 ($replace)...

Proposed resolution

Update AdContentBucket::postAdRender() to safely handle nulls by casting to string and defaulting to an empty string when the #ad_impression_id is not set. Example fix:

$search  = (string) TrackerInterface::PLACEHOLDER_IMPRESSION;
$replace = isset($build['#ad_impression_id']) ? (string) $build['#ad_impression_id'] : '';
$subject = (string) $markup;

$output = str_replace($search, $replace, $subject);

return $markup instanceof \Drupal\Component\Render\MarkupInterface
  ? \Drupal\Component\Render\Markup::create($output)
  : $output;

Remaining tasks

  • [ ] Attach patch with fix.
  • [ ] Review by maintainers.
  • [ ] Add test coverage to ensure postAdRender() works without #ad_impression_id.

Note: I will attach a patch to this issue, as I cannot currently provide a Merge Request on GitLab for this project.

User interface changes

None.

API changes

None (only internal implementation hardened).

Data model changes

None.

πŸ› Bug report
Status

Active

Version

11.0

Component

ad module

Created by

πŸ‡ΊπŸ‡ΈUnited States dobe

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

Comments & Activities

Production build 0.71.5 2024