Score:0

Unable to programmatically render webform with CAPTCHA

ng flag

Our website has a custom route, with a controller rendering a custom Webform. This is required, as the form has some hidden fields to be pre-populated.

Now I would like to add the CAPTCHA module to the webform. When enabling the CAPTCHA for Drupal's default contact form and viewing it in incognito mode, it works nicely. The CAPTCHA shows up.

But when adding the CAPTCHA element to the custom form, it is not displayed at all.

I've tried several ways to embed the form, but none is working:

  • Embed as a Webform entity (using getSubmissionForm(..))
  • Embed as a Webform entity (using getViewBuilder('webform'))
  • Embed as a render array with the new element type webform

Would be glad to get any info on what's wrong here?

sonfd avatar
in flag
Are there any JS errors in console? Is the element rendered on the page, but not visible (i.e. in the HTML source)? Does the CAPTCHA display when you render the webform normally, and only fails on your custom route when you render the webform programmatically?
david avatar
ng flag
@sonfd nope, no JS errors. There is also nothing CAPTCHA related in the HTML source. I'm quite sure the problem is on the Drupal /backend-side. Now, I've also created a standard page which includes the webform. And no, de CAPTCHA doesn't display there either. When selecting the standard contact form instead, it works.
Joehoe avatar
tr flag
Are you visiting the custom form anonymous?
Alireza Tabatabaeian avatar
cn flag
how did you attached the captcha to your custom form?
david avatar
ng flag
@Joehoe yes, see above in my question - I'm viewing it in "incognito mode". The standard contact form works this way, the custom one not.
david avatar
ng flag
@AlirezaTabatabaeian I've tried both ways: 1. adding the CAPTCHA element in the form editing mode and 2. adding it via the CAPTCHA module settings by assigning it as a default to the form node type.
Score:0
ng flag

It turned out there is a custom twig template in the theme, specifically overwriting this form only. In this template only some specific form elements are rendered:

/web/themes/custom/some_theme/templates/webfom/webform--course-registration.html.twig

This wasn't visible at first, because other templates related to this custom form implementation are located in the custom form module.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.