\Drupal\Component\Render\FormattableMarkup::placeholderFormat() triggers fatal error

Created on 17 January 2016, almost 9 years ago
Updated 1 August 2024, 5 months ago

Problem/Motivation

When the string in question has an invalid placeholder, placeholderFormat() throws an E_USER_ERROR, which according to the PHP docs behaves like E_ERROR and is a "fatal" error.

          // We do not trigger an error for placeholder that start with an
          // alphabetic character.
          if (!ctype_alpha($key[0])) {
            // We trigger an error as we may want to introduce new placeholders
            // in the future without breaking backward compatibility.
            trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_ERROR);
          }

In my case, the error was killing the script execution, which seems unnecessary. Are these errors really "fatal"? Won't they simply result in strings that aren't correctly rendered? I marked as "major" because it seems in some cases a minor coding mistake could result in lost data, etc. due to script abortion.

To repeat

I haven't been able to reliably reproduce it yet. In my case, the chain appears to start in devel_module.php, backtrace_error_handler().

PHP message: PHP Fatal error:  Invalid placeholder (!message) in string: %type: !message in %function (line %line of %file). in /var/www/html/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 240
PHP message: PHP Stack trace:
...
PHP message: PHP  26. Drupal\Core\Logger\LoggerChannel->log() /var/www/html/modules/contrib/devel/devel.module:375
PHP message: PHP  27. Drupal\dblog\Logger\DbLog->log() /var/www/html/core/lib/Drupal/Core/Logger/LoggerChannel.php:108
PHP message: PHP  28. Drupal\Core\Logger\LogMessageParser->parseMessagePlaceholders() /var/www/html/core/modules/dblog/src/Logger/DbLog.php:67
PHP message: PHP  29. strpos() /var/www/html/core/lib/Drupal/Core/Logger/LogMessageParser.php:21
PHP message: PHP  30. Drupal\Core\StringTranslation\TranslatableMarkup->__toString() /var/www/html/core/lib/Drupal/Core/Logger/LogMessageParser.php:21
PHP message: PHP  31. Drupal\Core\StringTranslation\TranslatableMarkup->render() /var/www/html/core/lib/Drupal/Component/Utility/ToStringTrait.php:20
PHP message: PHP  32. Drupal\Component\Render\FormattableMarkup::placeholderFormat() /var/www/html/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php:209
PHP message: PHP  33. trigger_error() /var/www/html/core/lib/Drupal/Component/Render/FormattableMarkup.php:240

Proposed resolution

Change to E_USER_WARNING instead of E_USER_ERROR:

          // We do not trigger an error for placeholder that start with an
          // alphabetic character.
          if (!ctype_alpha($key[0])) {
            // We trigger an error as we may want to introduce new placeholders
            // in the future without breaking backward compatibility.
            trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_WARNING);
          }
πŸ› Bug report
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
RenderΒ  β†’

Last updated 3 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States kentr

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

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