Problem/Motivation

If a CAPTCHA module is not quite how we would like on our site, it is useful to alter it.

Proposed resolution

Add a new hook hook_captcha_alter().

Remaining tasks

If a maintainer welcomes this then I would write a patch.

Call goes in processCaptchaElement() right after the call to invoke.

User interface changes

None

API changes

New hook

Data model changes

None

CommentFileSizeAuthor
#2 captcha.alter_.3250708-2.patch1.85 KBadamps

Issue fork captcha-3250708

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

AdamPS created an issue. See original summary.

adamps’s picture

Status: Active » Needs review
StatusFileSize
new1.85 KB
anybody’s picture

If you'd like to alter the UI, this issue might also be relevant: #3314766: [2.x] Improve the CAPTCHA form markup and use twig files for more flexibility, where possible and a better approach for theming than the FAPI handling. So I guess we need both! :)

anybody’s picture

Status: Needs review » Postponed (maintainer needs more info)

@AdamPS: Isn't it possible to use hook_form_alter instead for this? Does this need an extra hook?

adamps’s picture

Status: Postponed (maintainer needs more info) » Needs review

@AdamPS: Isn't it possible to use hook_form_alter instead for this? Does this need an extra hook?

No unfortunately not. When hook_form_alter() is called, form array contains the result of captcha_form_alter(), which looks something like this:

$captcha_element = [
          '#type' => 'captcha',
          '#captcha_type' => $captcha_point->getCaptchaType(),
        ];

The part we need to alter only gets created very late, during the final render stage.

#3314766: [2.x] Improve the CAPTCHA form markup and use twig files for more flexibility, where possible is a good idea and I look forward to it in 2.x. For the moment we only have 1.x so it would be great to have a solution that works now, thanks.

anybody’s picture

@AdamPS thanks! Then I'd agree we should add that hook from #2!

Haven't tested it yet, but we should consider that!

anybody’s picture

@AdamPS could you perhaps create a MR for final review?

anybody’s picture

Version: 8.x-1.x-dev » 2.x-dev

Let's add this in 2.x, I think.

anybody’s picture

Assigned: Unassigned » grevil

@Grevil if you also think this makes sense, let's add this to 2.x please after your review.

anybody’s picture

anybody’s picture

@AdamPS: Please have a look at the MR. I just rerolled your patch from #2 as MR.

One thing I'm not sure about is, if we'd also may need a way to alter the $info prior to the captcha method invocation. What do you think?

In your case you need the already generated captcha?

grevil’s picture

Assigned: grevil » Unassigned
adamps’s picture

Status: Needs review » Reviewed & tested by the community

Great thanks.

One thing I'm not sure about is, if we'd also may need a way to alter the $info prior to the captcha method invocation. What do you think?

Beg pardon I don't understand what you mean by "captcha method invocation". If you are talking about
\Drupal::moduleHandler()->invoke($captcha_type_module, 'captcha' then this function doesn't actually get passed the $info, so it's not needed.

Would be great to have a backport to 1.x please seeing as 2.x is not yet ready for use in production. It seems like a totally safe fix.

  • Anybody committed 9ac97517 on 2.x
    Issue #3250708 by Anybody, AdamPS: Allow altering of CAPTCHA from other...
anybody’s picture

Status: Reviewed & tested by the community » Fixed

Thank you @AdamPS, Merged!

Could you provide a MR to merge this into 8.x-1.x? Cherry-pick doesn't seem to work and I don't have much time currently.

anybody’s picture

Version: 2.x-dev » 8.x-1.x-dev
Status: Fixed » Active

Switching to 8.x-1.x for that.

adamps’s picture

Status: Active » Needs review

Thanks here is the MR.

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @AdamPS! Merged in 2022 :P

  • Anybody committed 6f320307 on 8.x-1.x authored by AdamPS
    Issue #3250708 by Anybody, AdamPS, Grevil: Allow altering of CAPTCHA...
anybody’s picture

Status: Reviewed & tested by the community » Fixed
adamps’s picture

Version: 8.x-1.x-dev » 2.x-dev

Great thanks

Status: Fixed » Closed (fixed)

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