Problem/Motivation
Following #2807743: Switch from deprecation notice to warning for non-standard placeholders in FormattableMarkup::placeholderFormat() we now get deprecation messages at 9.1 when placeholders do not have a required prefix character. However, it seems that there are valid situations where placeholder prefixes are not required, and cases where the placeholder name has to be a specific string without any prefix, such as in the $context array.
See Rules issue #3172046: [10] Fix automated test results - Support for keys without a placeholder prefix is deprecated (Rules) comments #3 and #4 for more details.
Proposed resolution
Investigate whether the current criteria for determining the placeholders is too strict and produces deprecation warnings that cannot be resolved or corrected.
Remaining tasks
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | Screenshot 2023-08-21 at 2.52.14 PM.png | 108.73 KB | smustgrave |
| #8 | 3173103_8_placeholders_without_key_prefix.patch | 794 bytes | aludescher |
Issue fork drupal-3173103
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #6
larowlanIs this still relevant?
Comment #7
aludescher commentedYes, still relevant.
LoggerChannel adds some placeholders without prefix in the keys which are than reported as deprecated by Formattablemarkup.
This causes error: "Support for keys without a placeholder prefix is deprecated in Drupal 9.1.0 ...".
But if '@uid' parameter is also set the error will be "Invalid placeholder (%s) with string: ...".
Comment #8
aludescher commentedThis patch is just a basic workaround!
Comment #10
tr commentedThis has become a much bigger problem, because this now generates a warning when running D10 (see #3295625: Remove deprecated code from FormattableMarkup), causing test failures.
I discuss in #3172046: [10] Fix automated test results - Support for keys without a placeholder prefix is deprecated (Rules) why this core behavior is wrong:
additionally mentioned in that issue:
Note since the time I originally posted this api.drupal.org has stopped hosting the API doc page for the Symfony LoggerInterface, so you'll have to find that on your own ...
Comment #12
tolstoydotcomI tried to duplicate this on D11 and it worked as expected, without warnings. What's a snippet that shows the problem?
Comment #13
prabuela commentedComment #14
prabuela commentedComment #15
tolstoydotcomI was able to see warnings by following the instructions at #3347741: D10 "Invalid placeholder (context) with string" error
The merge request I added to this issue only calls trigger_error if a key starts with a non-alphanumeric character. A key of
contextwouldn't cause a warning, but*contextwould.Comment #16
j. commentedAm i missing something? i see the issue fork, but no merge request/patch to test.
Comment #17
tolstoydotcomI might have done the merge request wrong, but the only file I changed is this:
https://git.drupalcode.org/issue/drupal-3173103/-/blob/3173103-false-pos...
I don't think that has changed much between versions so you can probably just post it into a D10/D11 installation.
Or, click 'Show commands' above and follow those instructions.
Comment #18
j. commentedThanks TolstoyDotCom. I'm using views_conditional and have been having issues with this (https://www.drupal.org/project/views_conditional/issues/3347741#comment-...), but can confirm that the changes in #15 stop the error messages that were flooding the logs. For the record, the site is using 10.0.10 and views_conditional 1.5.
Comment #20
tobas1996 commented#17 I created the MR, now you can put just the .diff :
https://git.drupalcode.org/project/drupal/-/merge_requests/4528.diff
Thanks!
Comment #21
j. commentedThanks. patch applied cleanly to 10.1.2
Comment #22
boby_ui commented+1 patch works well for 10.1.2
Comment #23
tolstoydotcomPlease note that the patch from #8 and my changes in #15 are different, mine is a more generalized solution.
I've since changed core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php to deal with the changes to core/lib/Drupal/Component/Render/FormattableMarkup.php. Hopefully that will cause the test to succeed. If anyone has any ideas for other tests please let me know.
Comment #24
tolstoydotcomIs #15 a reasonable solution or does anyone have a better way to do this?
Comment #25
smustgrave commentedNice addition of ctype_alnum.
Lets see what committers say!
Verified the tests fail without the fix too.
Comment #26
longwaveI think we could almost consider dropping the warning entirely, but there is some benefit in keeping it for users that previously were used to the ! syntax that is no longer allowed. As we ignore valid placeholders that aren't present in the string, ignoring entirely-invalid placeholders that don't begin with a special character also seems fine.
Backported to 10.1.x as a low risk bug fix.
Committed and pushed e1a599bedd to 11.x and 08fa3c2ea2 to 10.1.x. Thanks!
Comment #30
donquixote commentedRelated: #3352384: Add Exception for TypeError Argument must be String in Drupal Component Utility Html escape{}
A question is whether we should already validate and filter placeholder arguments in FormattableMarkup::__construct(), or wait for __toString() to be called as it is now.
Doing it in __construct() will provide a more useful backtrace.
However, if there is any code that relies on non-Drupal placeholders returned from FormattableMarkup->getArguments(), even if they won't be inserted in the string, that code would no longer work as designed.
So maybe we can validate in __construct() to call trigger_error(), but filter in ::placeholderFormat() which is called in __toString(), where we then don't call trigger_error() again.