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
•
19 Apr 2024
Extending a Contrib Module's Configuration in Drupal 10: A Case Study with Sitewide Alert
Extending the functionality of a contrib module in Drupal can enhance its usefulness without altering the original codebase. This article provides a step-by-step guide on how to add custom configuration options to an existing contrib module, using the sitewide_alert module as an example. We will...
Andrew Fletcher
•
18 Apr 2024
LogicException: Form errors cannot be set after form validation has finished
When receiving a logic error such asLogicException: Form errors cannot be set after form validation has finished. in Drupal\Core\Form\FormState->setErrorByName() (line 1104 of /var/www/html/project/core/lib/Drupal/Core/Form/FormState.php).This error "LogicException: Form errors cannot be set...
Andrew Fletcher
•
09 Apr 2024
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
Adding a new service to a custom module and the following error greeted me:The website encountered an unexpected error. Try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "summaries.reference_node". in...