Skip to content

Commit

Permalink
Merge pull request DieSchittigs#14 from bennyborn/master
Browse files Browse the repository at this point in the history
Improve handling of palettes in DCA (fixes DieSchittigs#10)
  • Loading branch information
Lixus authored Mar 11, 2020
2 parents 3d24354 + d0a7f4f commit fa43f13
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
18 changes: 12 additions & 6 deletions src/Resources/contao/dca/tl_form_field.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?php

// alter palette for use with reCaptcha V3
$GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'][] = function() {

if( \Config::get('recaptchaType') != 'recaptcha3' ) {
return;
}

$GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha'] = str_replace('{fconfig_legend},', '{fconfig_legend},recaptcha3_threshold,recaptcha3_action,', $GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha']);
};

$GLOBALS['TL_DCA']['tl_form_field']['fields'] += [
'recaptch3_threshold' => [
'recaptcha3_threshold' => [
'label' => &$GLOBALS['TL_LANG']['tl_form_field']['recaptcha3_threshold'],
'inputType' => 'text',
'sql' => "varchar(8) NOT NULL default ''",
Expand All @@ -13,8 +23,4 @@
'sql' => "varchar(120) NOT NULL default ''",
'eval' => ['tl_class' => 'w50', 'rgxp' => 'recaptcha']
],
];

if (Config::get('recaptchaType') != 'recaptcha3') return;

$GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha'] = str_replace(',type,label', ',type,label,recaptcha3_threshold,recaptcha3_action', $GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha']);
];
22 changes: 14 additions & 8 deletions src/Resources/contao/dca/tl_settings.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
<?php

$palette = $GLOBALS['TL_DCA']['tl_settings']['palettes'];
$palette = isset($palette['default']) ? $palette['default'] : $palette;
$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] = str_replace('{files_legend', '{recaptcha_legend},recaptchaType;{files_legend', $GLOBALS['TL_DCA']['tl_settings']['palettes']['default']);

$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] = str_replace('{files_legend', '{recaptcha_legend},recaptchaType,recaptchaPublicKey,recaptchaPrivateKey;{files_legend', $palette);
$GLOBALS['TL_DCA']['tl_settings']['palettes']['recaptcha3'] = str_replace('{files_legend', '{recaptcha_legend},recaptchaType,recaptcha3GlobalThreshold,recaptchaPublicKey,recaptchaPrivateKey;{files_legend', $palette);
$GLOBALS['TL_DCA']['tl_settings']['config']['onload_callback'][] = function() {

$GLOBALS['TL_DCA']['tl_settings']['palettes']['__selector__'][] = 'recaptchaType';
switch( \Config::get('recaptchaType') ) {

case 'recaptcha3':
$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] = str_replace('recaptchaType','recaptchaType,recaptcha3GlobalThreshold,recaptchaPublicKey,recaptchaPrivateKey', $GLOBALS['TL_DCA']['tl_settings']['palettes']['default']);
break;
default:
$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] = str_replace('recaptchaType','recaptchaType,recaptchaPublicKey,recaptchaPrivateKey', $GLOBALS['TL_DCA']['tl_settings']['palettes']['default']);
break;
}
};

$GLOBALS['TL_DCA']['tl_settings']['fields'] += [
'recaptchaType' => [
'label' => &$GLOBALS['TL_LANG']['tl_settings']['recaptchaType'],
'inputType' => 'select',
'options_callback' => function ()
'options_callback' => function ()
{
return [
'invisible' => 'reCAPTCHA v2: Invisible',
'recaptcha2' => 'reCAPTCHA v2: Checkbox',
'invisible' => 'reCAPTCHA v2: Invisible',
'recaptcha2' => 'reCAPTCHA v2: Checkbox',
'recaptcha3' => 'reCAPTCHA v3',
];
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php if ($this->recaptchaType != 'recaptcha3') return; ?>

<script src="https://www.google.com/recaptcha/api.js?onload=onRecaptchaLoadCallbackModule<?= $this->id ?>&render=<?= $this->publicKey ?>" defer async></script>
<script type="text/javascript" defer async>
<script>
function onRecaptchaLoadCallbackModule<?= $this->id ?> () {
grecaptcha.execute('<?= $this->publicKey ?>', {action: '<?= $this->action ?>'})
}
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/templates/form_recaptcha.html5
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<p class="error"><?= $this->getErrorAsString() ?></p>
<?php endif; ?>

<div class="g-recaptcha"
<div class="g-recaptcha"
id="recaptcha-<?= $this->strCaptchaKey ?>"
data-sitekey="<?= $this->publicKey ?>"
data-callback="onSubmit_<?= $this->strCaptchaKey ?>"
<?php if ($this->recaptchaType == 'invisible'): ?> data-size="invisible" <?php endif; ?>
></div>

<script src="https://www.google.com/recaptcha/api.js?onload=onRecaptchaLoadCallbackField<?= $this->strCaptchaKey ?><?php if ($this->recaptchaType == 'recaptcha3'): ?>&render=<?= $this->publicKey ?><?php endif; ?>" defer async></script>
<script type="text/javascript" defer async>
<script>
function onRecaptchaLoadCallbackField<?= $this->strCaptchaKey ?> () {
window.onSubmit_<?= $this->strCaptchaKey ?> = function () { };

Expand Down

0 comments on commit fa43f13

Please sign in to comment.