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
- Update to PHP 8.2
- Go to create a shorthand entity
- Select a shorthand story from the dropdown
- Click on save
- Note the deprecation warning (also present in watchdog)
For str_replace
- Update to PHP 8.2
- Go to create a shorthand entity
- Select a shorthand story from the dropdown
- Clear the contents of the Thumbnail field
- Click on save
- 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.