PHP8 Deprecated function : htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated

Created on 4 August 2022, almost 2 years ago
Updated 25 October 2023, 7 months ago

Problem/Motivation

I have a contact form with the subject field hidden, filled later in a HOOKmail_alter()
I get a PHP debug log with PHP8 when sending mail via contact forms :
Deprecated function : htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated dans Drupal\Component\Utility\Html::escape() (/project/web/core/lib/Drupal/Component/Utility/Html.php ligne 424)

Full stackrace :

Deprecated function : htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated dans Drupal\Component\Utility\Html::escape() (/project/web/core/lib/Drupal/Component/Utility/Html.php ligne 424)
#0 /project/web/core/includes/bootstrap.inc(346): _drupal_error_handler_real(8192, 'htmlspecialchar...', '/project/web/co...', 424)
#1 [internal function]: _drupal_error_handler(8192, 'htmlspecialchar...', '/project/web/co...', 424)
#2 /project/web/core/lib/Drupal/Component/Utility/Html.php(424): htmlspecialchars(NULL, 11, 'UTF-8')
#3 /project/web/core/lib/Drupal/Component/Render/FormattableMarkup.php(262): Drupal\Component\Utility\Html::escape(NULL)
#4 /project/web/core/lib/Drupal/Component/Render/FormattableMarkup.php(208): Drupal\Component\Render\FormattableMarkup::placeholderEscape(NULL)
#5 /project/web/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php(195): Drupal\Component\Render\FormattableMarkup::placeholderFormat('[@form] @subjec...', Array)
#6 /project/web/core/lib/Drupal/Component/Utility/ToStringTrait.php(15): Drupal\Core\StringTranslation\TranslatableMarkup->render()
#7 /project/web/core/modules/contact/contact.module(151): Drupal\Core\StringTranslation\TranslatableMarkup->__toString()
#8 /project/web/core/lib/Drupal/Core/Mail/MailManager.php(275): contact_mail('page_autoreply', Array, Array)
#9 /project/web/core/lib/Drupal/Core/Mail/MailManager.php(180): Drupal\Core\Mail\MailManager->doMail('contact', 'page_autoreply', 'dsi-infra-front...', 'fr', Array, NULL, true)
#10 /project/web/core/lib/Drupal/Core/Render/Renderer.php(564): Drupal\Core\Mail\MailManager->Drupal\Core\Mail\{closure}()
#11 /project/web/core/lib/Drupal/Core/Mail/MailManager.php(181): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#12 /project/web/modules/contrib/mailsystem/src/MailsystemManager.php(70): Drupal\Core\Mail\MailManager->mail('contact', 'page_autoreply', 'dsi-infra-front...', 'fr', Array, NULL, true)
#13 /project/web/core/modules/contact/src/MailHandler.php(131): Drupal\mailsystem\MailsystemManager->mail('contact', 'page_autoreply', 'dsi-infra-front...', 'fr', Array)
#14 /project/web/core/modules/contact/src/MessageForm.php(223): Drupal\contact\MailHandler->sendMailMessages(Object(Drupal\contact\Entity\Message), Object(Drupal\Core\Session\AccountProxy))
#15 [internal function]: Drupal\contact\MessageForm->save(Array, Object(Drupal\Core\Form\FormState))
#16 /project/web/core/lib/Drupal/Core/Form/FormSubmitter.php(114): call_user_func_array(Array, Array)
#17 /project/web/core/lib/Drupal/Core/Form/FormSubmitter.php(52): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#18 /project/web/core/lib/Drupal/Core/Form/FormBuilder.php(592): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#19 /project/web/core/lib/Drupal/Core/Form/FormBuilder.php(320): Drupal\Core\Form\FormBuilder->processForm('contact_message...', Array, Object(Drupal\Core\Form\FormState))
#20 /project/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(48): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\contact\MessageForm), Object(Drupal\Core\Form\FormState))
#21 /project/web/modules/contrib/contact_storage/src/ContactFormViewBuilder.php(92): Drupal\Core\Entity\EntityFormBuilder->getForm(Object(Drupal\contact\Entity\Message))
#22 /project/web/modules/contrib/contact_storage/src/Controller/ContactStorageController.php(45): Drupal\contact_storage\ContactFormViewBuilder->view(Object(Drupal\contact\Entity\ContactForm), 'full', Object(Drupal\Core\Language\Language))
#23 [internal function]: Drupal\contact_storage\Controller\ContactStorageController->contactSitePage(Object(Drupal\contact\Entity\ContactForm))
#24 /project/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#25 /project/web/core/lib/Drupal/Core/Render/Renderer.php(564): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#26 /project/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#27 /project/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#28 /project/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#29 /project/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#30 /project/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /project/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /project/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#33 /project/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#34 /project/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#35 /project/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#36 /project/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#37 /project/web/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#38 /project/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#39 {main}

.

Steps to reproduce

  • Be sure to run on PHP8 (in my case 8.1.7)
  • Create a contact form
  • In the form-display, disable the subject field
  • Submit the form
  • Check your dblogs.

Proposed resolution

Just control Null values on the subject field.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Contact 

Last updated 1 day ago

Created by

🇫🇷France pguillard

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024