Allow adding expired and error-callback attributes to recaptcha element

Created on 6 August 2025, 2 months ago

Problem/Motivation

Recaptcha allows adding expired and error callback attributes: https://developers.google.com/recaptcha/docs/display#render_param

Currently this is possible by implementing hook captcha alter:

/**
 * Implements hook_captcha().
 */
function hook_captcha_alter(&$captcha, $info) {
  // $form['elements']['captcha']['#attributes']['data-expired-callback'] = 'recaptchaExpired';
  if ($info['module'] == 'recaptcha') {
    $captcha['form']['recaptcha_widget']['#markup'] = preg_replace(
        '/<div([^>]*)>/',
        '<div$1 data-expired-callback="onRecaptchaExpired">',
        $captcha['form']['recaptcha_widget']['#markup'],
        1 // only replace the first occurrence
    );
    $captcha['form']['captcha_response']['#description'] = t('New description.');
  }
}

This is not ideal, but gets the job done.

But the recaptcha.js converts the attributes to camelCase using this.data(). Google expects these to be in kebab-case.

Steps to reproduce

1. Implement hook_captcha_alter to add new attributes
2. The callbacks will fail due to recaptcha.js using camelCase instead of kebab-case

Proposed resolution

Update recaptcha.js to use kebab-case

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

3.0

Component

reCAPTCHA V2

Created by

🇮🇳India dipakmdhrm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dipakmdhrm
  • @dipakmdhrm opened merge request.
Production build 0.71.5 2024