This one is pretty simple but actually threw me for a fair bit. This is for anyone who has a form that registers newsletter subscribers with a checkbox.
Customise as you need it.
Include the hook into your formit call. I called mine 'cmHook'
Setup your form HTML.
<form action="[[~[[*id]]]]" method="post">
<input type="hidden" name="nospam" value="" />
<fieldset>
<input type="text" name="Name" value="[[!+fi.Name]]" placeholder="Name*">
<input type="text" name="EmailAddress" value="[[!+fi.EmailAddress]]" placeholder="Email*">
<input type="checkbox" name="Subscription" value="Yes" checked="checked" [[!+fi.Subscription:FormItIsChecked=`Yes`]] /> Subscribe?
</fieldset>
<input type="submit" value="Request">
</form>
Snippet (cmHook):
<?php
require_once $modx->getOption('base_path') . 'assets/components/csapi/csrest_subscribers.php';
$values = $hook->getValues();
$Subscription = $values['Subscription'];
if ( $Subscription == Yes ) {
echo
$wrap = new CS_REST_Subscribers('LIST-ID', 'API-KEY');
$result = $wrap->add(
$allFormFields = $hook->getValues(array(
'EmailAddress' => 'EmailAddress',
'Name' => 'Name',
'Resubscribe' => true
))
);
} else {
echo "";
}
return true;
You'll need to replace LIST-ID and API-KEY with your own details. The list ID is found by going into 'Lists & Subscribers' and selecting your list and then clicking 'change name/type', then at the bottom you'll see 'API Subscriber List ID'. To find the API key you'll need to go to account settings and click manage API keys. There you'll find a list of API keys per list that you've created.
Finally you'll also need to download this:
https://github.com/campaignmonitor/createsend-php
Upload it to assets/components/csapi/