Contao 4.4: Newsletter-Abo mit Datenschutz-Abfrage

(Kommentare: 0)

Im Artikel contao-3-newsletter-abonnement-captcha.html ging es darum, das Newsletter-Abo vor Spam zu schützen. Contao 4 hat das captcha nun integriert. Aber für den Fall, dass man noch die Datenschutzfrage stellen möchte (Checkbox "Ja, ich habe die Infos zum Datenschutz gelesen..."), kann man analog vorgehen.

 
Dazu müsste man ein lokales Bundle anlegen unter ROOT/src, vgl. https://gist.github.com/joergmoldenhauer/84dab927446e316c2e02760e4506b86a).

 

Dort drin:

 

ROOT/src/MyNameSpace/MyNameSpaceBundle/src/Resources/contao/config/config.php:

<?php 
$GLOBALS['FE_MOD']['newsletter']['subscribe'] = '\MyNameSpace\ModuleSubscribeDataPrivacy';
$GLOBALS['FE_MOD']['newsletter']['unsubscribe'] = '\MyNameSpace\ModuleUnsubscribeDataPrivacy';
?>


ROOT/templates/nl_default.html (Kopie von ROOT/vendor/contao/newsletter-bundle/src/Resources/contao/templates/newsletter) erweitern um folgende Zeilen nach <?= $this->captcha ?>:

<?php if ($this->checkbox): ?>
    <?php echo $this->checkbox ?>
<?php endif ?>


ROOT/src/MyNameSpace/MyNameSpaceBundle/src/Resources/contao/modules/ModuleSubscribeDataPrivacy.php:

<?php
namespace MyNameSpace;

/** Extension of ModuleSubscribe to add data security checkbox to newsletter subscribe form, Contao 4.4.
  * @author Ingmar Decker
  */
class ModuleSubscribeDataPrivacy extends \ModuleSubscribe
{
    protected $objCheckbox;
    
    protected function compile()
    {
        $this->objCheckbox = new \Contao\FormCheckBox(array(
            'id'=> 'data_'.$this->id, 
            'label' => $GLOBALS['TL_LANG']['MSC']['nl_subscribt_data_label'], 
            'options' => array(array('value' => 1, 'label' => $GLOBALS['TL_LANG']['MSC']['nl_subscribt_data_option'])),
            'mandatory' => 1,
            'tableless' => 1
        ));
        parent::compile();
        $this->Template->checkbox = $this->objCheckbox->parse();
    }
    
    protected function validateForm(\Contao\Widget $objWidget=null)
    {
        if ($this->objCheckbox->hasErrors()) {
            $this->Template->mclass = 'error';
            $this->Template->message = $this->objCheckbox->getErrorsAsString();
            return false;
        }
        return parent::validateForm($objWidget);
    }
}
?>


ROOT/src/MyNameSpace/MyNameSpaceBundle/src/Resources/contao/modules/ModuleUnsubscribeDataPrivacy.php:

 
<?php
namespace MyNameSpace;

/** Extension of ModuleUnsubscribe to add data security checkbox to newsletter subscribe form, Contao 4.4.
  * @author Ingmar Decker
  */
class ModuleUnsubscribeDataPrivacy extends \ModuleUnsubscribe
{
    protected $objCheckbox;
    
    protected function compile()
    {
        $this->objCheckbox = new \Contao\FormCheckBox(array(
            'id'=> 'data_'.$this->id, 
            'label' => $GLOBALS['TL_LANG']['MSC']['nl_unsubscribt_data_label'], 
            'options' => array(array('value' => 1, 'label' => $GLOBALS['TL_LANG']['MSC']['nl_unsubscribt_data_option'])),
            'mandatory' => 1,
            'tableless' => 1
        ));
        parent::compile();
        $this->Template->checkbox = $this->objCheckbox->parse();
    }
    
    protected function validateForm(\Contao\Widget $objWidget=null)
    {
        if ($this->objCheckbox->hasErrors()) {
            $this->Template->mclass = 'error';
            $this->Template->message = $this->objCheckbox->getErrorsAsString();
            return false;
        }
        return parent::validateForm($objWidget);
    }
}
?>


ROOT/src/MyNameSpace/MyNameSpaceBundle/src/Resources/contao/languages/en/default.php:

<?php 
$GLOBALS['TL_LANG']['MSC']['nl_subscribt_data_label'] = 'Data privacy';
$GLOBALS['TL_LANG']['MSC']['nl_subscribt_data_option'] = 'I have read the <a href="{{link_url::1234}}" title="Data privacy" target="_blank">data privacy statement</a>. I agreee, that my entries are saved permanently and used for sending the newsletter.<br>Advice: You can revoke this agreement any time by unsubscribung from the newsletter.';
$GLOBALS['TL_LANG']['MSC']['nl_unsubscribt_data_label'] = 'Data privacy';
$GLOBALS['TL_LANG']['MSC']['nl_unsubscribt_data_option'] = 'I have taken note of the  <a href="{{link_url::1234}}" title="Data privacy" target="_blank">data privacy statement</a>. The e-mail address will be sent to the server and then deleted.';
?>

Darin natürlich die 1234 durch die ID der Datenschurtz-Seite ersetzen...!

 

 

 

Zurück

Kommentare

Einen Kommentar schreiben