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.

Comments

wsantell created an issue. See original summary.

wsantell’s picture

wsantell’s picture

Issue summary: View changes
tr’s picture

Version: 2.0.2 » 2.0.x-dev
Status: Patch (to be ported) » Needs review
tr’s picture

Status: Needs review » Needs work
wsantell’s picture

I 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

tr’s picture

You 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?

wsantell’s picture

Do you want this?

$preview_string = (string) t('Preview');
if ($triggering_element['#value'] == $preview_string) {

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.

tr’s picture

Version: 2.0.x-dev » 2.1.x-dev
wsantell’s picture

The attached patch is updated for 2.1.x, using the 2.1.1 branch.

tr’s picture

Status: Needs work » Needs review

  • TR committed 924789e on 2.1.x authored by wsantell
    Issue #3265111 by wsantell: Notice reported when checkbox triggers AJAX
    
tr’s picture

Status: Needs review » Fixed

Thanks. Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.