class CampaignMonitorListSettingsForm in Campaign Monitor 8.2
Same name and namespace in other branches
- 8 src/Form/CampaignMonitorListSettingsForm.php \Drupal\campaignmonitor\Form\CampaignMonitorListSettingsForm
Configure campaignmonitor settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\campaignmonitor\Form\CampaignMonitorListSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of CampaignMonitorListSettingsForm
1 string reference to 'CampaignMonitorListSettingsForm'
File
- src/
Form/ CampaignMonitorListSettingsForm.php, line 19
Namespace
Drupal\campaignmonitor\FormView source
class CampaignMonitorListSettingsForm extends ConfigFormBase {
/**
* The campaign monitor manager.
*
* @var Drupal\campaignmonitor\CampaignMonitorManager
*/
protected $campaignMonitorManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The module manager service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Drupal\Core\Config\ConfigFactoryInterface definition.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Class constructor.
*/
public function __construct(CampaignMonitorManager $campaignmonitor_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
$this->campaignMonitorManager = $campaignmonitor_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static($container
->get('campaignmonitor.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'campaignmonitor_list_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'campaignmonitor.settings.list',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $list_id = NULL) {
$form = [
'#tree' => TRUE,
];
// Reset lists so that we can make changes with this form.
// This will ensure that settings come direct from CM and not cache.
$this->campaignMonitorManager
->getLists([], TRUE);
$defaults = $this->campaignMonitorManager
->getListSettings($list_id);
$list = $this->campaignMonitorManager
->getExtendedList($list_id);
// Add list id to the form.
$form['listId'] = [
'#type' => 'hidden',
'#value' => $list_id,
];
// Set this form name (index).
$form_key = 'campaignmonitor_list_' . $list_id;
$form[$form_key]['status'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Enable list'),
'#description' => $this
->t('Enable the list to configure it and use it on the site.'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form[$form_key]['status']['enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable'),
'#default_value' => isset($defaults['status']['enabled']) ? $defaults['status']['enabled'] : 0,
'#attributes' => [
'class' => [
'enabled-list-checkbox',
],
],
];
$form[$form_key]['options'] = [
'#type' => 'fieldset',
'#title' => $this
->t('List options'),
'#description' => $this
->t('Changing the values will result in an update of the values on the Campaign Monitor homepage.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => [
'visible' => [
'.enabled-list-checkbox' => [
'checked' => TRUE,
],
],
],
];
$form[$form_key]['options']['listname'] = [
'#type' => 'textfield',
'#title' => $this
->t('List name'),
'#default_value' => $list['name'],
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="status[enabled]"]' => [
'checked' => TRUE,
],
],
],
];
$form[$form_key]['options']['UnsubscribePage'] = [
'#type' => 'textfield',
'#title' => $this
->t('Unsubscribe page'),
'#default_value' => $list['details']['UnsubscribePage'],
];
$form[$form_key]['options']['ConfirmationSuccessPage'] = [
'#type' => 'textfield',
'#title' => $this
->t('Confirmation success page'),
'#default_value' => $list['details']['ConfirmationSuccessPage'],
];
$form[$form_key]['options']['ConfirmedOptIn'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Confirmed Opt In'),
'#description' => $this
->t('Selecting this will mean that subscribers will need to confirm their email each time they
subscribe to the list'),
'#default_value' => $list['details']['ConfirmedOptIn'],
];
$form[$form_key]['display'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Display options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => [
'visible' => [
'.enabled-list-checkbox' => [
'checked' => TRUE,
],
],
],
];
$form[$form_key]['display']['name'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display Name field'),
'#description' => $this
->t('Whether the Name field should be displayed when subscribing.'),
'#default_value' => isset($defaults['display']['name']) ? $defaults['display']['name'] : 0,
'#attributes' => [
'class' => [
'tokenable',
'tokenable-name',
],
],
];
$form[$form_key]['display']['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#description' => $this
->t('A description to accompany the list in forms.'),
'#default_value' => isset($defaults['display']['description']) ? $defaults['display']['description'] : '',
'#attributes' => [
'class' => [
'tokenable',
'tokenable-description',
],
],
];
$field_map = $this->entityFieldManager
->getFieldMap();
$user_field_map = $field_map['user'];
$user_fields = array_keys($user_field_map);
array_unshift($user_fields, 'None');
foreach ($user_fields as $key => $user_field) {
unset($user_fields[$key]);
$user_fields[$user_field] = $user_field;
}
if (!empty($list['CustomFields'])) {
$options = [];
foreach ($list['CustomFields'] as $key => $field) {
// Form API can't handle keys with [] in all cases.
$token_form_key = str_replace([
'[',
']',
], '', $key);
$user_fields[$token_form_key] = 'Custom Field: ' . $field['FieldName'];
}
}
$form[$form_key]['display']['fname_field'] = [
'#type' => 'select',
'#options' => $user_fields,
'#title' => $this
->t('First name field'),
'#description' => $this
->t('The name that will be used by Campaign Monitor as a salutation in emails sent out. If using custom fields, they will not appear unless selected in custom field config below.'),
'#default_value' => isset($defaults['display']['fname_field']) ? $defaults['display']['fname_field'] : 0,
];
$form[$form_key]['display']['lname_field'] = [
'#type' => 'select',
'#options' => $user_fields,
'#title' => $this
->t('Last name field'),
'#description' => $this
->t('The name that will be used by Campaign Monitor as a salutation in emails sent out. If using custom fields, they will not appear unless selected in custom field config below or none to leave empty.'),
'#default_value' => isset($defaults['display']['lname_field']) ? $defaults['display']['lname_field'] : 0,
];
// List custom fields.
if (!empty($list['CustomFields'])) {
$options = [];
foreach ($list['CustomFields'] as $key => $field) {
// Form API can't handle keys with [] in all cases.
$token_form_key = str_replace([
'[',
']',
], '', $key);
$options[$token_form_key] = $field['FieldName'];
}
$form[$form_key]['CustomFields'] = [
'#type' => 'table',
'#caption' => $this
->t('Custom fields - drag to arrange order'),
'#header' => [
$this
->t('Include'),
$this
->t('Require'),
$this
->t('Weight'),
],
'#empty' => $this
->t('No items.'),
'#tableselect' => FALSE,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'group-order-weight',
],
],
'#states' => [
'visible' => [
'.enabled-list-checkbox' => [
'checked' => TRUE,
],
],
],
];
// Use defaults if exists to get the order correct.
if (isset($defaults['CustomFields'])) {
$new_options = [];
foreach ($defaults['CustomFields'] as $key => $values) {
$new_options[$key] = $options[$key];
}
$options = $new_options;
}
foreach ($options as $key => $option) {
$form[$form_key]['CustomFields'][$key]['#attributes']['class'][] = 'draggable';
$form[$form_key]['CustomFields'][$key]['#weight'] = $defaults['CustomFields'][$key]['weight'];
$form[$form_key]['CustomFields'][$key]['selected'] = [
'#type' => 'checkbox',
'#title' => $option,
'#default_value' => isset($defaults['CustomFields'][$key]['selected']) ? $defaults['CustomFields'][$key]['selected'] : 0,
'#attributes' => [
'class' => [
'tokenable',
'tokenable-custom-fields',
],
],
];
$form[$form_key]['CustomFields'][$key]['required'] = [
'#type' => 'checkbox',
'#title' => $option,
'#default_value' => isset($defaults['CustomFields'][$key]['required']) ? $defaults['CustomFields'][$key]['required'] : 0,
];
$form[$form_key]['CustomFields'][$key]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $option,
]),
'#title_display' => 'invisible',
'#default_value' => isset($defaults['CustomFields'][$key]['weight']) ? $defaults['CustomFields'][$key]['weight'] : 0,
'#attributes' => [
'class' => [
'group-order-weight',
],
],
];
}
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*
* Edit form validation handler which calls the API to save the information
* that was entered. This is done in the validation function so we can give
* better feedback to the user and to prevent the user from having to enter
* the information once more on failure.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$list_id = $form_state
->getValue('listId');
$values = $form_state
->getValues();
// Build array with basic information.
$values = $values['campaignmonitor_list_' . $list_id];
$options = [
'Title' => HTML::escape($values['options']['listname']),
'UnsubscribePage' => HTML::escape($values['options']['UnsubscribePage']),
'ConfirmedOptIn' => $values['options']['ConfirmedOptIn'] ? TRUE : FALSE,
'ConfirmationSuccessPage' => HTML::escape($values['options']['ConfirmationSuccessPage']),
];
$result = $this->campaignMonitorManager
->updateList($list_id, $options);
if ($result != 'success') {
$form_state
->setErrorByName('', $result);
}
// Redirect to list overview.
$url = Url::fromRoute('campaignmonitor.lists');
$form_state
->setRedirectUrl($url);
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$list_id = $form_state
->getValue('listId');
$values = $form_state
->getValues();
$values = $values['campaignmonitor_list_' . $list_id];
// These are saved remotely.
unset($values['options']);
$list_config = $this->configFactory
->getEditable('campaignmonitor.settings.list');
$list_key = $this->campaignMonitorManager
->listKey($list_id);
$list_config
->set($list_key, $values)
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CampaignMonitorListSettingsForm:: |
protected | property | The campaign monitor manager. | |
CampaignMonitorListSettingsForm:: |
protected | property |
Drupal\Core\Config\ConfigFactoryInterface definition. Overrides FormBase:: |
|
CampaignMonitorListSettingsForm:: |
protected | property | The entity field manager. | |
CampaignMonitorListSettingsForm:: |
protected | property | The entity type manager. | |
CampaignMonitorListSettingsForm:: |
protected | property | The module manager service. | |
CampaignMonitorListSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
CampaignMonitorListSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
CampaignMonitorListSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
CampaignMonitorListSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CampaignMonitorListSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
CampaignMonitorListSettingsForm:: |
public | function |
Edit form validation handler which calls the API to save the information
that was entered. This is done in the validation function so we can give
better feedback to the user and to prevent the user from having to enter
the information once more on… Overrides FormBase:: |
|
CampaignMonitorListSettingsForm:: |
public | function |
Class constructor. Overrides ConfigFormBase:: |
|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |