campaignmonitor_registration.module in Campaign Monitor 7
Same filename and directory in other branches
Newsletter subscription for users on the registration page.
File
modules/campaignmonitor_registration/campaignmonitor_registration.moduleView source
<?php
/**
* @file
* Newsletter subscription for users on the registration page.
*/
/**
* Implements hook_perm().
*/
function campaignmonitor_registration_permission() {
return [
'access campaign monitor registration' => [
'title' => t('Access Campaign Monitor on registration page'),
'description' => t('Allow user to subscribe to lists on the registration page.'),
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Hooks into the registration form and adds the option to select one of the
* available Campaign Monitor lists on registration.
*/
function campaignmonitor_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
// Get connected to the API and retrieve the lists.
$cm = CampaignMonitor::getConnector();
$lists = $cm
->getLists();
// Build options for the form selector.
$options = [];
foreach ($lists as $list_id => $list) {
// Check if the list is selected to be shown.
$list_options = variable_get('campaignmonitor_list_' . $list_id, []);
if (campaignmonitor_is_list_enabled($list_id) && $list_options && isset($list_options['display']['registration']) && $list_options['display']['registration']) {
$options[$list_id] = $list['name'];
}
}
if (!empty($options)) {
$form['campaignmonitor'] = [
'#type' => 'fieldset',
'#title' => t('News lists'),
];
$defaults = variable_get('campaignmonitor_general', []);
$form['campaignmonitor']['campaignmonitor_lists'] = [
'#type' => 'checkboxes',
'#description' => !empty($defaults['instructions']) ? t('%instructions', [
'%instructions' => $defaults['instructions'],
]) : t('Select the news lists that you want to subscribe to.'),
'#options' => $options,
];
}
$form['#submit'][] = 'campaignmonitor_registration_form_user_register_submit';
}
/**
* Submission form handler.
*
* Information about the selected list will be
* submitted to Campaign Monitor.
*/
function campaignmonitor_registration_form_user_register_submit($form, &$form_state) {
if (isset($form_state['values']['campaignmonitor_lists']) && !form_get_errors()) {
// Get connected to the API and get lists.
$cm = CampaignMonitor::getConnector();
$lists_info = $cm
->getLists();
// Find the selected lists, if any.
foreach ($form_state['values']['campaignmonitor_lists'] as $list_id => $selected) {
if ($selected) {
// Try to subscribe to the list.
if (!$cm
->subscribe($list_id, check_plain($form_state['values']['mail']), check_plain($form_state['values']['name']))) {
form_set_error('', t('You were not subscribed to the list. Please try again later.'));
$form_state['redirect'] = FALSE;
return FALSE;
}
drupal_set_message(t('You are now subscribed to the "@list" list.', [
'@list' => $lists_info[$list_id]['name'],
]), 'status');
}
}
// Remove it from the form before the submit functions take over (might not
// be needed).
unset($form_state['values']['campaignmonitor_lists']);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function campaignmonitor_registration_form_campaignmonitor_admin_settings_list_edit_alter(&$form, &$form_state, $form_id) {
// Find form key to index the form array and load defaults.
$form_key = 'campaignmonitor_list_' . $form['listId']['#value'];
$defaults = variable_get($form_key, []);
// Add option to enable this form on the user page.
$form[$form_key]['display']['registration'] = [
'#type' => 'checkbox',
'#title' => t('Display list on registration page'),
'#description' => t('Enable this list on the registration page and allow subscription.'),
'#default_value' => isset($defaults['display']['registration']) ? $defaults['display']['registration'] : 0,
];
}
Functions
Name | Description |
---|---|
campaignmonitor_registration_form_campaignmonitor_admin_settings_list_edit_alter | Implements hook_form_FORM_ID_alter(). |
campaignmonitor_registration_form_user_register_form_alter | Implements hook_form_FORM_ID_alter(). |
campaignmonitor_registration_form_user_register_submit | Submission form handler. |
campaignmonitor_registration_permission | Implements hook_perm(). |