From 47df5198f3da260e2f2072bbee43ce69656a928e Mon Sep 17 00:00:00 2001 From: rang501 Date: Thu, 17 Dec 2015 09:37:47 +0200 Subject: [PATCH] RA0023-153 --- recaptcha/drupal-recaptcha.js | 28 ++++++++++++++++++++++++++++ recaptcha/recaptcha.module | 20 ++++++++------------ recaptcha/recaptcha.test | 2 +- 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 recaptcha/drupal-recaptcha.js diff --git a/recaptcha/drupal-recaptcha.js b/recaptcha/drupal-recaptcha.js new file mode 100644 index 0000000..6d4cee9 --- /dev/null +++ b/recaptcha/drupal-recaptcha.js @@ -0,0 +1,28 @@ +(function ($, window, document) { + +Drupal.behaviors.recaptcha = { + attach: function (context, settings) { + $('.g-recaptcha', context).each(function() { + if (typeof grecaptcha == 'undefined') { + return; + } + if ($(this).hasClass('drupal-recaptcha-processed')) { + grecaptcha.reset(); + } + else { + grecaptcha.render(this, $(this).data()); + $(this).addClass('drupal-recaptcha-processed'); + } + + }); + } +} + +window.drupalRecaptchaOnload = function() { + $('.g-recaptcha').each(function() { + grecaptcha.render(this, $(this).data()); + $(this).addClass('drupal-recaptcha-processed'); + }); +} +})(jQuery, window, document); + diff --git a/recaptcha/recaptcha.module b/recaptcha/recaptcha.module index 1e65aab..10e4d4b 100644 --- a/recaptcha/recaptcha.module +++ b/recaptcha/recaptcha.module @@ -114,20 +114,16 @@ function recaptcha_captcha($op, $captcha_type = '') { $captcha['form']['recaptcha_widget'] = array( '#markup' => '', '#suffix' => $noscript, - ); - - // @todo: #1664602: D7 does not yet support "async" in drupal_add_js(). - // drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external')); - $data = array( - '#tag' => 'script', - '#value' => '', - '#attributes' => array( - 'src' => url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), - 'async' => 'async', - 'defer' => 'defer', + '#attached' => array( + 'js' => array( + drupal_get_path('module', 'recaptcha') . '/drupal-recaptcha.js', + array( + 'data' => url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language, 'render' => 'explicit', 'onload' => 'drupalRecaptchaOnload'), 'absolute' => TRUE)), + 'type' => 'external', + ), + ), ), ); - drupal_add_html_head($data, 'recaptcha_api'); } else { // Fallback to Math captcha as reCAPTCHA is not configured. diff --git a/recaptcha/recaptcha.test b/recaptcha/recaptcha.test index 4afedf7..ea641c0 100644 --- a/recaptcha/recaptcha.test +++ b/recaptcha/recaptcha.test @@ -128,7 +128,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { // Check if there is a reCAPTCHA on the login form. $this->drupalGet('user'); $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); - $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); + $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); $this->assertNoRaw($grecaptcha . '