Deprecated function: htmlspecialchars(): Passing null to parameter #1

Created on 8 September 2022, about 2 years ago
Updated 7 June 2024, 6 months ago

Problem/Motivation

Following PHP debug error shows up after saving a JSON:API Resource overwrites.

Deprecated function: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Component\Utility\Html::escape() (line 424 of core/lib/Drupal/Component/Utility/Html.php).
Drupal\Component\Utility\Html::escape(NULL) (Line: 262)
Drupal\Component\Render\FormattableMarkup::placeholderEscape(NULL) (Line: 232)
Drupal\Component\Render\FormattableMarkup::placeholderFormat('Created the %label JSON:API Resource overwrites.', Array) (Line: 195)
Drupal\Core\StringTranslation\TranslatableMarkup->render() (Line: 15)
Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (Line: 54)
Drupal\Core\Messenger\Messenger->addMessage(Object, 'status', ) (Line: 73)
Drupal\Core\Messenger\Messenger->addStatus(Object) (Line: 210)
Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('jsonapi_resource_config_add_form', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Drupal Version
9.3.16
PHP version
8.1.8

Steps to reproduce

  1. Turn on all message to display on the 'Logging and errors' configuration form (/admin/config/development/logging)
  2. Go to the 'JSON:API Resource overrides' configuration form (/admin/config/services/jsonapi/resource_types)
  3. Choose a resource type to edit, for example 'user--user' (/admin/config/services/jsonapi/resource_types/user--user/edit)
  4. Save the configuration.

Proposed resolution

This error comes from Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm->save(Array, Object),

$resource_config = $this->entity;
    $status = $resource_config->save();

    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created the %label JSON:API Resource overwrites.', [
          '%label' => $resource_config->label(),
        ]));
        break;

      default:
        $this->messenger()->addStatus($this->t('Saved the %label JSON:API Resource overwrites.', [
          '%label' => $resource_config->label(),
        ]));
    }
    $form_state->setRedirectUrl($resource_config->toUrl('collection'));

$resource_config->label() returns null for some reasons.
Using 'resource type' instead of the config entity label would be more meaningful.
The message after saving would look like

Saved the user--user JSON:API Resource overwrites.

So change the code above to

    $resource_config = $this->entity;
    $status = $resource_config->save();
    $type = $resource_config->get('resourceType') ?? '';

    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created the %label JSON:API Resource overwrites.', [
        '%label' => $type,
        ]));
        break;

      default:
        $this->messenger()->addStatus($this->t('Saved the %label JSON:API Resource overwrites.', [
        '%label' => $type,
        ]));
    }
    $form_state->setRedirectUrl($resource_config->toUrl('collection'));
  }

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇦🇺Australia mingsong 🇦🇺

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

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.71.5 2024