1

On my magento 2.4 I've enabled magento's google recaptcha from the security settings and I have enabled it for newsletter subscription form.

On my website, I have two instances of the subscription form: one in footer, and another one in the home page.

Both calls the default Subscribe block, but the footer form is the default subscribe.phtml template while for the other one I create a phtml template in the design folder, then created a cms static block that calls this template with:

{{block class="Magento\Newsletter\Block\Subscribe" name="static.newsletter" template="Magento_Newsletter::static-subscribe.phtml"}}

On the footer form, everything works as expected, including the invisible captcha, while on the other form, the form itself works but there is no captcha.

How can I have the captcha on the custom form as well?

On magento default recaptcha-newsletter module, magento called the recaptcha block by a referenceContainer to the footer handle. My custom template, however, is not inside the footer handle, therefore the recaptcha block is not called. I think I have to create a custom xml layout, call the appropriate handle and call the recaptcha block from there, but I don't know what the appropriate handle is for a cms block. I tried with a referenceBlock to both the identifier and the name of the block that I set on the backend, but none work.

1
  • did you solve this issue? Nov 28, 2022 at 13:03

2 Answers 2

0

I have used an approach similar to the following to create CMS page-based registration pages with invisible reCaptcha.

I adapted this work to create a test that is more similar to your use-case, where a CMS block renders the invisible reCaptcha.

However, I did not test whether this specific example fully functions as needed, or prevents conflicts when there are multiple reCaptcha-enabled forms on the same page.

If you create a custom Block class, e.g. [Vendor][Namespace]\Block\Captcha, you can can get the necessary reCaptcha components with a method such as:

// The class must make $this->storeManager and $this->scopeConfig available.

public function toHtml() {
    $storeId = $this->storeManager->getStore()->getId();
    $siteKey = $this->scopeConfig->getValue('msp_securitysuite_recaptcha/general/public_key', 'store', $storeId);
    return $this->getLayout()->createBlock(
        "Magento\ReCaptchaUi\Block\ReCaptcha",
        "recaptcha-newsletter",
        [
            "data" => [
                "recaptcha_for" => "newsletter",
                "jsLayout" => [
                    "components" => [
                        "recaptcha" => [
                            "component" => "Magento_ReCaptchaFrontendUi\/js\/reCaptcha"
                        ]
                    ]
                ]
            ]
        ]
    )->setTemplate('Magento_ReCaptchaNewsletter::recaptcha_newsletter.phtml')->toHtml();
}

This is block is equivalent to the one configured in vendor/magento/module-re-captcha-newsletter/view/frontend/layout/default.xml.

You can then pull this block into your CMS block:

{{block class="\[Vendor]\[Namespace]\Block\Captcha"}}

Where [Vendor][Namespace]\Block\Captcha is the block with the custom toHtml() method.

3
  • Thank you, I like your solution, far better that mine (I used jquery to append google recaptcha to the form instead of the main block.. very ugly!) I'll try yours as soon as possible! Thanks again
    – Lou Nik
    Sep 20, 2021 at 8:07
  • @LouNik did you solve this issue? Nov 25, 2022 at 6:40
  • @SavanPatel hi, sorry for the late reply I didn't see your comment. Yes, I have tried a couple of ways, including jiheison's . I like jiheison's solution better than mine, it's a lot cleaner, so I'd suggest you can try that one, it worked for me
    – Lou Nik
    Feb 20 at 14:10
0

Your solution is not working.

This is the only solution, i.e. the register-recaptcha can be inserted as a block:

echo $this->getLayout()
->createBlock('MSP\ReCaptcha\Block\Frontend\ReCaptcha',
    'msp-recaptcha',
    ['data' =>
        [
            'jsLayout' =>
                [
                    'components' =>
                        [
                            'msp-recaptcha' =>
                                [
                                    'component' => 'MSP_ReCaptcha/js/reCaptcha',
                                    'reCaptchaId' => 'msp-recaptcha',
                                    'zone' => 'create'
                                ]
                        ]
                ]
        ]
    ]
)
->setTemplate('MSP_ReCaptcha::msp_recaptcha.phtml')
->toHtml();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.