PHP 8.2 deprecations for mb_convert_encoding and str_replace

Created on 10 December 2024, about 1 month ago

Problem/Motivation

When updating to PHP 8.2 the following deprecation warnings are thrown when saving a shorthand entity:

Deprecated function: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in Drupal\shorthand\Entity\ShorthandStory->preSave() (line 128 of /var/www/docroot/modules/contrib/shorthand/src/Entity/ShorthandStory.php)

#0 /var/www/docroot/core/includes/bootstrap.inc(166): _drupal_error_handler_real(8192, 'mb_convert_enco...', '/var/www/docroo...', 128)
#1 [internal function]: _drupal_error_handler(8192, 'mb_convert_enco...', '/var/www/docroo...', 128)
#2 /var/www/docroot/modules/contrib/shorthand/src/Entity/ShorthandStory.php(128): mb_convert_encoding('<meta charset="...', 'HTML-ENTITIES', 'UTF-8')
#3 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(528): Drupal\shorthand\Entity\ShorthandStory->preSave(Object(Drupal\shorthand\ShorthandStoryStorage))
#4 /var/www/docroot/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(753): Drupal\Core\Entity\EntityStorageBase->doPreSave(Object(Drupal\shorthand\Entity\ShorthandStory))
#5 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(483): Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object(Drupal\shorthand\Entity\ShorthandStory))
#6 /var/www/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\shorthand\Entity\ShorthandStory))
#7 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\shorthand\Entity\ShorthandStory))
#8 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityForm.php(293): Drupal\Core\Entity\EntityBase->save()
#9 /var/www/docroot/modules/contrib/shorthand/src/Form/ShorthandStoryForm.php(166): Drupal\Core\Entity\EntityForm->save(Array, Object(Drupal\Core\Form\FormState))
#10 [internal function]: Drupal\shorthand\Form\ShorthandStoryForm->save(Array, Object(Drupal\Core\Form\FormState))

And then if creating a new Shorthand Story deleting the Thumbnail field:

Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in Drupal\shorthand\Entity\ShorthandStory->preSave() (line 175 of /var/www/docroot/modules/contrib/shorthand/src/Entity/ShorthandStory.php)

#0 /var/www/docroot/core/includes/bootstrap.inc(166): _drupal_error_handler_real(8192, 'str_replace(): ...', '/var/www/docroo...', 175)
#1 [internal function]: _drupal_error_handler(8192, 'str_replace(): ...', '/var/www/docroo...', 175)
#2 /var/www/docroot/modules/contrib/shorthand/src/Entity/ShorthandStory.php(175): str_replace('{Shorthand Loca...', 'https://local.m...', NULL)
#3 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(528): Drupal\shorthand\Entity\ShorthandStory->preSave(Object(Drupal\shorthand\ShorthandStoryStorage))
#4 /var/www/docroot/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(753): Drupal\Core\Entity\EntityStorageBase->doPreSave(Object(Drupal\shorthand\Entity\ShorthandStory))
#5 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(483): Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object(Drupal\shorthand\Entity\ShorthandStory))
#6 /var/www/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\shorthand\Entity\ShorthandStory))
#7 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\shorthand\Entity\ShorthandStory))
#8 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityForm.php(293): Drupal\Core\Entity\EntityBase->save()
#9 /var/www/docroot/modules/contrib/shorthand/src/Form/ShorthandStoryForm.php(166): Drupal\Core\Entity\EntityForm->save(Array, Object(Drupal\Core\Form\FormState))
#10 [internal function]: Drupal\shorthand\Form\ShorthandStoryForm->save(Array, Object(Drupal\Core\Form\FormState))

Steps to reproduce

For mb_convert_encoding deprecation

  1. Update to PHP 8.2
  2. Go to create a shorthand entity
  3. Select a shorthand story from the dropdown
  4. Click on save
  5. Note the deprecation warning (also present in watchdog)

For str_replace

  1. Update to PHP 8.2
  2. Go to create a shorthand entity
  3. Select a shorthand story from the dropdown
  4. Clear the contents of the Thumbnail field
  5. Click on save
  6. Note the deprecation warning (also present in watchdog)

Proposed resolution

https://php.watch/versions/8.2/mbstring-qprint-base64-uuencode-html-enti... suggests replacing it with the htmlspecialchars_decode() and htmlentities() methods.

I think we can do that for both the head and body.

Then for the deprecation regarding str_replace is just a matter of making sure we are not trying to replace a NULL variable.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇦🇷Argentina anairamzap Buenos Aires

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024