Problem/Motivation
A notice is reported in dblog if a custom form triggers AJAX from a checkbox element.
Notice: Object of class Drupal\Core\StringTranslation\TranslatableMarkup could not be converted to int in _honeypot_time_restriction_validate() (line 229 of /app/modules/baseline/contrib/honeypot/honeypot.module)
I've not investigated other triggers besides SELECT elements, which do not cause this issue.
Steps to reproduce
Create an AJAX form where clicking a checkbox triggers the AJAX callback. The issue appears to be this code from honeypot.module (in _honeypot_time_restriction_validate()):
if ($triggering_element['#value'] == t('Preview')) {
return;
}
The default value for checkbox is 1, so it is comparing "1 == TranslatableMarkup object"
Proposed resolution
Derive the translated string before comparing it against $triggering_element['#value']:
$preview_string = t('Preview')->__toString();
if ($triggering_element['#value'] == $preview_string) {
return;
}
Patch attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3265111-10-deliberate-string-comparison-of-preview.patch | 536 bytes | wsantell |
| #6 | 3265111-6--deliberate-string-comparison-of-preview.patch | 540 bytes | wsantell |
| #2 | 3265111--deliberate-string-comparison-of-preview.patch | 546 bytes | wsantell |
Comments
Comment #2
wsantell commentedComment #3
wsantell commentedComment #4
tr commentedComment #5
tr commentedComment #6
wsantell commentedI had patched against 2.0.2.
New patch is patched against Development version: 2.0.x-dev updated 16 Feb 2022 at 01:40 UTC
Comment #7
tr commentedYou should never call a magic method directly like that. Use a cast instead. And will this work in all cases? Or perhaps the code should compare something other than the #value element?
Comment #8
wsantell commentedDo you want this?
or this?
if ($triggering_element['#value'] == (string) t('Preview')) {or maybe this?
if ((string) $triggering_element['#value'] == t('Preview')) {As for comparing against something else, I would guess there isn't a better target. This code checks if the trigger is a Preview button, but the button/submit array doesn't have an index value which can be set to indicate "this is a preview button" - you have to check if the button's text is "Preview" and that text comes from #value.
Comment #9
tr commentedComment #10
wsantell commentedThe attached patch is updated for 2.1.x, using the 2.1.1 branch.
Comment #11
tr commentedComment #13
tr commentedThanks. Committed.