PHP 8.1+ Deprecation warning: Passing null to json_decode() in _cookies_addons_views_is_restricted()

Created on 14 April 2025, 12 days ago

Problem/Motivation

When using the Cookies Addons Views module with PHP 8.1 or higher, a deprecation warning appears in the logs:

Deprecated function: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in _cookies_addons_views_is_restricted() (line 110 of modules/contrib/cookies_addons/modules/cookies_addons_views/cookies_addons_views.module)

The issue occurs because the module passes a potential null value directly to `json_decode()`. In PHP 8.1+, passing null to the first parameter of `json_decode()` is deprecated and will eventually become an error in future PHP versions.

Steps to reproduce

1. Install and enable the Cookies Addons Views module
2. Use PHP 8.1 or higher
3. Visit any page where views are rendered
4. Check the error logs for deprecation warnings

The issue occurs in the `_cookies_addons_views_is_restricted()` function where the code attempts to decode the "cookiesjsr" cookie value without checking if it exists:

$cookiesJson = \Drupal::request()->cookies->get('cookiesjsr');
$cookies = json_decode($cookiesJson, TRUE);

If the "cookiesjsr" cookie doesn't exist, the `$cookiesJson` variable will be null, triggering the deprecation warning when passed to `json_decode()`.

Proposed resolution

Check if the cookie value exists before attempting to decode it:

$cookiesJson = \Drupal::request()->cookies->get('cookiesjsr');
$cookies = !empty($cookiesJson) ? json_decode($cookiesJson, TRUE) : [];

This ensures that `json_decode()` is only called with a valid string value and provides an empty array as a fallback when the cookie is not set.

Remaining tasks

  • Apply the fix in the `cookies_addons_views.module` file
  • Test with PHP 8.1 and PHP 8.2 to ensure no more deprecation warnings appear
  • Test with various cookie states to ensure functionality remains intact

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.2

Component

Cookies Addons Views

Created by

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

Comments & Activities

Production build 0.71.5 2024