Skip to main content

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