Hi, guys.

I had a ajax link, on click I insert form in html. In this form I have reCAPTCHA field, reCAPTCHA doesn't shown in this case.

There are anonymized html:

<div class="captcha">
<input type="hidden" name="captcha_sid" value="11111">
<input type="hidden" name="captcha_token" value="11111">
<input type="hidden" name="captcha_response" value="Google no captcha">
<div class="g-recaptcha" data-sitekey="111111111111111" data-theme="light" data-type="image" data-tabindex="2"></div>
</div>

As I see in recaptcha_captcha drupal_add_html_head is used and it's not working for ajax.

I add checking if it's ajax call, and in this case use drupal_add_js instead of drupal_add_html_head, it's work for me.
Could you please check the patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

voron created an issue. See original summary.

hass’s picture

Category: Feature request » Bug report
Status: Active » Closed (duplicate)
Issue tags: -Ajax
Parent issue: » #2493183: Ajax support / Use behaviors
voron’s picture

Issue summary: View changes
voron’s picture

Unfortunately my path dosen't work in case if user close popup with form and open it again. I'll try to work around.

trebormc’s picture

this patch work correctly with the module Asaf in D7
https://www.drupal.org/project/asaf

Thanks
Edit: My error, only works on first ajax load

trebormc’s picture

FileSize
1.29 KB

new patch to reload recaptcha in all ajax calls

mattjones86’s picture

Status: Closed (duplicate) » Needs review
Parent issue: #2493183: Ajax support / Use behaviors »
Related issues: +#2493183: Ajax support / Use behaviors

This shouldn't have been marked as a duplicate, since the referenced parent issue is for the 8.x branch. If someone can find a duplicate 7.x issue and link it here then feel free to close again.

What I would say about the patch is that it still throws an 'Undefined method' error on first page load. It should also be checking for the existence of grecaptcha.render in case everything has not fully initialised at the time of load.

It works great for ajax requests though.

greggles’s picture

Title: reCAPTCHA + Ajax » Drupal 7.x: Ajax support / Use behaviors for 2.x

@mattjones86: it's common to fix issues in one branch and then mark the issue as needs porting to the other branch, which I guess is what hass was doing. If this is going to be open separately, it seems worthwhile to make it really clear from the title that it's 90% the same, just for a different branch.

TwoD’s picture

FYI: There's also a patch for this in #2493183-153: Ajax support / Use behaviors which works well.
It's also using #attached which is preferred over calling drupal_add_js().

Joao Sausen’s picture

#6 worked well here

Joao Sausen’s picture

deleted

Joao Sausen’s picture

dench0’s picture

Here the fixed patch from #12 to apply it within the module directory.

Fabsgugu’s picture

Status: Needs review » Reviewed & tested by the community

Patch #13 work for me.

Steven Jones’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
5.73 KB
3.67 KB

I'm going to mark this back as needing review, and take a patch from the corresponding D8 issue to move this one along, specifically I'm working from #2493183-168: Ajax support / Use behaviors.
I've fixed the broken test from that patch, and put back the defer attribute on the script that that patch removed, the only other real change is that the tag is going to lose the async attribute, but I've included support for that once #1664602: Allow attributes to be passed to drupal_add_[css|js] (SRI) lands.

This patch is superior to the one in #13 here because:

  • The external recaptcha script is also added to the page on an AJAX request.
  • It uses the recaptcha methods to reset recaptcha if it's been initialized on an element, rather than simply blatting the DOM.

Status: Needs review » Needs work

The last submitted patch, 15: recaptcha-ajax-support-2875698-15.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Steven Jones’s picture

Status: Needs work » Needs review
FileSize
5.73 KB
1.88 KB

Doh! Copy/paste fail there.

torotil’s picture

Status: Needs review » Needs work
Related issues: +#1664602: Allow attributes to be passed to drupal_add_[css|js] (SRI)

I’ve had a look at the code and think that the overall approach looks good. There are a few code-style issues there though. Although the existing recaptcha isn’t exactly coding-style compliant at least new code should be.

I’m also marking the core issue as related.

james.williams’s picture

Could you be more specific about what the code style issues that you're referring to are please?

torotil’s picture

Could you be more specific about what the code style issues that you're referring to are please?

Sorry, for the vague comment. Here are is the relevant output of phpcs (based on the current 7.x-2.x):

FILE: /home/roman/code/drupal/recaptcha/recaptcha.module
---------------------------------------------------------------------------------------------------------------------------------------------
 135 | ERROR   | [ ] The array declaration extends to column 181 (the limit is 80). The array content should be split up over multiple lines
 135 | ERROR   | [ ] The array declaration extends to column 160 (the limit is 80). The array content should be split up over multiple lines
 139 | WARNING | [ ] Line exceeds 80 characters; contains 84 characters


FILE: /home/roman/code/drupal/recaptcha/recaptcha.test
---------------------------------------------------------------------------------------------------------------------------------------------
 134 | WARNING | [ ] Line exceeds 80 characters; contains 82 characters
 135 | WARNING | [ ] Line exceeds 80 characters; contains 290 characters
 135 | ERROR   | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
 147 | ERROR   | [x] There should be no white space after an opening "("
 149 | WARNING | [ ] Line exceeds 80 characters; contains 82 characters
 150 | WARNING | [ ] Line exceeds 80 characters; contains 296 characters
 150 | ERROR   | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses

For the commented out lines I would either ignore it or use /* … */ to silence phpcs.