View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\Element;
function campaignmonitor_registration_form_user_register_form_alter(&$form, FormStateInterface &$form_state) {
$lists = campaignmonitor_get_lists();
$config = Drupal::config('campaignmonitor_registration.settings');
$options = [];
$option_descriptions = [];
foreach ($lists as $list_id => $list) {
$list_options = campaignmonitor_get_list_settings($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'];
$defaults = campaignmonitor_get_list_settings($list_id);
if (isset($defaults['display']['description'])) {
$option_descriptions[$list_id] = $defaults['display']['description'];
}
}
}
if (!empty($options)) {
$form['campaignmonitor'] = [
'#type' => 'fieldset',
'#title' => t('News lists'),
];
$defaults = campaignmonitor_get_lists();
$form['campaignmonitor']['lists'] = [
'#type' => 'checkboxes',
'#description' => !empty($defaults['instructions']) ? t($defaults['instructions']) : t('Select the news lists that you want to subscribe to.'),
'#options' => $options,
'#option_descriptions' => $option_descriptions,
'#after_build' => array(
'_campaignmonitor_registration_option_descriptions',
),
];
}
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'campaignmonitor_registration_form_user_register_submit';
}
}
}
function _campaignmonitor_registration_option_descriptions($element, &$form_state) {
foreach (Element::children($element) as $key) {
$element[$key]['#description'] = t('@description', [
'@description' => $element['#option_descriptions'][$key],
]);
}
return $element;
}
function campaignmonitor_registration_form_user_register_submit(&$form, FormStateInterface &$form_state) {
$values = $form_state
->getValues();
if (isset($values['lists'])) {
$lists_info = campaignmonitor_get_lists();
$name = SafeMarkup::checkPlain($values['name']);
foreach ($values['lists'] as $list_id => $selected) {
if ($selected) {
$settings = campaignmonitor_get_list_settings($list_id);
if (isset($settings['display']['name_field']) && is_string($settings['display']['name_field']) && $settings['display']['name_field'] != '') {
if (isset($values[$settings['display']['name_field']][0]['value']) && $values[$settings['display']['name_field']][0]['value'] != '') {
$name = $values[$settings['display']['name_field']][0]['value'];
}
}
$subscribe_now = TRUE;
$result = Drupal::moduleHandler()
->invokeAll('campaignmonitor_registration_submit', [
$list_id,
$values['mail'],
]);
if (isset($result['subscribe']) && $result['subscribe'] === 0) {
$subscribe_now = FALSE;
}
if ($subscribe_now) {
if (!campaignmonitor_subscribe($list_id, SafeMarkup::checkPlain($values['mail']), SafeMarkup::checkPlain($name))) {
$form_state
->setErrorByName('', t('You were not subscribed to the list. Please try again later.'));
}
drupal_set_message(t('You are now subscribed to the "@list" list.', [
'@list' => html_entity_decode($lists_info[$list_id]['name']),
]), 'status');
}
}
}
}
}
function campaignmonitor_registration_form_campaignmonitor_list_settings_form_alter(&$form, FormStateInterface &$form_state) {
$list_id = $form['listId']['#value'];
$defaults = campaignmonitor_get_list_settings($list_id);
$form_key = 'campaignmonitor_list_' . $list_id;
$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,
];
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'campaignmonitor_registration_form_campaignmonitor_list_settings_submit';
}
}
}
function campaignmonitor_registration_form_campaignmonitor_list_settings_submit(&$form, FormStateInterface &$form_state) {
$list_id = $form['listId']['#value'];
$values = $form_state
->getValues();
$values = $values['campaignmonitor_list_' . $list_id];
unset($values['options']);
campaignmonitor_set_list_settings($list_id, $values);
}