Andrew Fletcher published: 16 January 2023 (updated) 17 January 2023 2 minutes read
A recent upgrade to PHP 8.1, has highlighted an error that hadn't been appearing previously.
Deprecated function : explode(): Passing null to parameter #2 ($string) of type string is deprecated in toc_js_node_view()(line 130 of modules/contrib/toc_js/toc_js.module).
In the top_js.module file from L124
// lambda function to clean css identifiers
$cleanCssIdentifier = function ($value) {
return Html::cleanCssIdentifier(trim($value));
};
// toc-js class is used to initialize the toc. Additional classes are added from the configuration.
$classes = array_map($cleanCssIdentifier, array_merge(['toc-js'], explode(',', $node_type->getThirdPartySetting('toc_js', 'classes') ?? '')));
$attributes = new Attribute(['class' => $classes]);
$attributes->setAttribute('id', 'toc-js-node-' . $entity->id());
$title_attributes = new Attribute(['class' => ['toc-title', 'h2']]);
if ($node_type->getThirdPartySetting('toc_js', 'sticky', 0)) {
$attributes->addClass('sticky');
}
The error as noted is on line 130, where the explode function is expecting a value or empty but not null.
To resolve this issue, I created a patch
diff -u --label a/web/modules/contrib/toc_js/toc_js.module \
--label b/web/modules/contrib/toc_js/toc_js-4321.module \
web/modules/contrib/toc_js/toc_js.module \
web/modules/contrib/toc_js/toc_js-4321.module > \
patches/toc_js-0197.patch
Where too_js-4321.module contains the change so the explode function will pass. The above function creates the following
diff --git a/toc_js.module b/toc_js.module
index 14ac7994fd..adffb6e9e7 100644
--- a/toc_js.module
+++ b/toc_js.module
@@ -127,7 +127,7 @@
};
// toc-js class is used to initialize the toc. Additional classes are added from the configuration.
- $classes = array_map($cleanCssIdentifier, array_merge(['toc-js'], explode(',', $node_type->getThirdPartySetting('toc_js', 'classes'))));
+ $classes = array_map($cleanCssIdentifier, array_merge(['toc-js'], explode(',', $node_type->getThirdPartySetting('toc_js', 'classes') ?? '')));
$attributes = new Attribute(['class' => $classes]);
$attributes->setAttribute('id', 'toc-js-node-' . $entity->id());
$title_attributes = new Attribute(['class' => ['toc-title', 'h2']]);
Drupal auto apply the patch
To auto-apply the patch in Drupal, you need to edit the composer.json file. Add the following lines to extra
"composer-exit-on-patch-failure": true,
"patchLevel": {
"drupal/core": "-p2"
},
"patches": {
"drupal/toc_js": {
"toc_js explode function": "patches/toc_js-0197.patch"
}
},
Related articles
Andrew Fletcher
•
04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher
•
26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher
•
17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...