Last active
August 25, 2020 09:39
-
-
Save oneNevan/69f8c0df7a6ebb8cb6ecf9c55aa42093 to your computer and use it in GitHub Desktop.
Magento 2. Extend MSP_ReCaptcha to add google reCaptcha to a Custom Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/etc/adminhtml/config.xml | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> | |
<default> | |
<msp_securitysuite_recaptcha> | |
<frontend> | |
<enabled_custom_form>1</enabled_brand_ambassador_application> | |
</frontend> | |
</msp_securitysuite_recaptcha> | |
</default> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/view/frontend/templates/custom_form.phtml | |
<?php | |
/** @var \Magento\Framework\View\Element\Template $block */ | |
?> | |
<form class="form custom_form" action="custom_form/index/submit" id="custom-form" method="post"> | |
<fieldset class="fieldset"> | |
<legend class="legend"><span><?= __('Custom Form') ?></span></legend> | |
<br> | |
<div class="field custom_field"> | |
<label class="label" for="name"><span><?= __('First Name') ?></span></label> | |
<div class="control"> | |
<input name="custom_field" id="custom_field" title="<?= __('Custom Field') ?>" value="" | |
class="input-text" type="text" | |
data-validate="{required:true}" aria-required="true"> | |
</div> | |
</div> | |
<?= $block->getChildHtml('form_additional_info') ?><!-- reCaptcha block is rendered here--> | |
</fieldset> | |
<div class="actions-toolbar"> | |
<div class="primary"> | |
<button type="submit" title="<?= __('Submit') ?>" class="action submit primary"> | |
<span><?= __('Submit') ?></span> | |
</button> | |
</div> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/view/frontend/layout/custom_form_index_index.xml | |
<?xml version="1.0"?> | |
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | |
<body> | |
<referenceContainer name="content"> | |
<block name="custom_form" template="Vendor_Module::custom_form.phtml"> | |
<container name="form.additional.info" as="form_additional_info"> | |
<block class="MSP\ReCaptcha\Block\Frontend\ReCaptcha" | |
template="MSP_ReCaptcha::msp_recaptcha.phtml" | |
name="msp-recaptcha" after="-" | |
ifconfig="msp_securitysuite_recaptcha/frontend/enabled"> | |
<arguments> | |
<argument name="jsLayout" xsi:type="array"> | |
<item name="components" xsi:type="array"> | |
<item name="msp-recaptcha" xsi:type="array"> | |
<item name="component" xsi:type="string">MSP_ReCaptcha/js/reCaptcha</item> | |
<item name="zone" xsi:type="const">Vendor\Module\Model\ReCaptchaConfig::ZONE_CUSTOM_FORM</item> | |
</item> | |
</item> | |
</argument> | |
</arguments> | |
</block> | |
</container> | |
</block> | |
</referenceContainer> | |
</body> | |
</page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/etc/frontend/di.xml | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<!-- Layout Settings Plugin --> | |
<type name="MSP\ReCaptcha\Model\LayoutSettings"> | |
<plugin name="msp_recaptcha_layout_settings_plugin" | |
type="Vendor\Module\Plugin\Model\LayoutSettingsPlugin" /> | |
</type> | |
<!-- Custom Form ReCaptcha --> | |
<virtualType name="Vendor\Module\Model\Provider\Failure\CustomFormObserver" | |
type="MSP\ReCaptcha\Model\Provider\Failure\ObserverRedirectFailure"> | |
<arguments> | |
<argument name="redirectUrlProvider" | |
xsi:type="object">MSP\ReCaptcha\Model\Provider\Failure\RedirectUrl\ReferrerUrlProvider</argument> | |
</arguments> | |
</virtualType> | |
<virtualType name="Vendor\Module\Model\Provider\IsCheckRequired\Frontend\CustomForm" | |
type="MSP\ReCaptcha\Model\IsCheckRequired"> | |
<arguments> | |
<argument name="enableConfigFlag" | |
xsi:type="string">msp_securitysuite_recaptcha/frontend/enabled_custom_form</argument> | |
<argument name="area" xsi:type="string">frontend</argument> | |
</arguments> | |
</virtualType> | |
<virtualType name="Vendor\Module\Observer\Frontend\CustomFormSubmitObserver" | |
type="MSP\ReCaptcha\Observer\ReCaptchaObserver"> | |
<arguments> | |
<argument name="isCheckRequired" | |
xsi:type="object">Vendor\Module\Model\Provider\IsCheckRequired\Frontend\CustomForm</argument> | |
<argument name="failureProvider" | |
xsi:type="object">Vendor\Module\Model\Provider\Failure\CustomFormObserver</argument> | |
</arguments> | |
</virtualType> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/etc/frontend/events.xml | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | |
<!-- custom_form_index_submit - full action path for POST form submit request --> | |
<event name="controller_action_predispatch_custom_form_index_submit"> | |
<observer name="msp_captcha" instance="Vendor\Module\Observer\Frontend\CustomFormSubmitObserver" /> | |
</event> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/Plugin/Model/LayoutSettingsPlugin.php | |
<?php | |
namespace Vendor\Module\Plugin\Model; | |
use MSP\ReCaptcha\Model\LayoutSettings; | |
use Vendor\Module\Model\ReCaptchaConfig; | |
class LayoutSettingsPlugin | |
{ | |
/** | |
* @var ReCaptchaConfig | |
*/ | |
private $config; | |
/** | |
* @param ReCaptchaConfig $config | |
*/ | |
public function __construct(ReCaptchaConfig $config) | |
{ | |
$this->config = $config; | |
} | |
/** | |
* @param LayoutSettings $subject | |
* @param array $result | |
* @return array | |
*/ | |
public function afterGetCaptchaSettings(LayoutSettings $subject, array $result) | |
{ | |
$result['enabled'][ReCaptchaConfig::ZONE_CUSTOM_FORM] = | |
$this->config->isEnabledFrontendZone(ReCaptchaConfig::ZONE_CUSTOM_FORM); | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/etc/module.xml | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | |
<module name="Vendor_Module"> | |
<sequence> | |
<module name="MSP_ReCaptcha" /> | |
</sequence> | |
</module> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/Model/ReCaptchaConfig.php | |
<?php | |
namespace Vendor\Module\Model; | |
use Magento\Framework\App\Config\ScopeConfigInterface; | |
use Magento\Store\Model\ScopeInterface; | |
class ReCaptchaConfig extends \MSP\ReCaptcha\Model\Config | |
{ | |
public const ZONE_CUSTOM_FORM = 'custom_form'; | |
/** | |
* @var ScopeConfigInterface | |
*/ | |
private $scopeConfig; | |
/** | |
* @param ScopeConfigInterface $scopeConfig | |
*/ | |
public function __construct(ScopeConfigInterface $scopeConfig) | |
{ | |
parent::__construct($scopeConfig); | |
$this->scopeConfig = $scopeConfig; | |
} | |
/** | |
* Check if reCaptcha is enabled for specified zone on frontend. | |
* | |
* @param string $zone | |
* @return bool | |
*/ | |
public function isEnabledFrontendZone(string $zone): bool | |
{ | |
if ($isEnabled = $this->isEnabledFrontend()) { | |
$isEnabled = $this->scopeConfig->isSetFlag( | |
static::XML_PATH_ENABLED_FRONTEND . '_' . $zone, | |
ScopeInterface::SCOPE_WEBSITE | |
); | |
} | |
return $isEnabled; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/registration.php | |
<?php | |
\Magento\Framework\Component\ComponentRegistrar::register( | |
\Magento\Framework\Component\ComponentRegistrar::MODULE, | |
'Vendor_Module', | |
__DIR__ | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app/code/Vendor/Module/etc/adminhtml/system.xml | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | |
<section id="msp_securitysuite_recaptcha"> | |
<group id="frontend"> | |
<field id="enabled_custom_form" translate="label" type="select" sortOrder="270" | |
showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1"> | |
<label>Use in Custom Form</label> | |
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | |
<depends> | |
<field id="enabled">1</field> | |
</depends> | |
</field> | |
</group> | |
</section> | |
</config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment