class WebformMailChimpHandler in Webform Mailchimp 8.5
Form submission to MailChimp handler.
Plugin annotation
@WebformHandler(
id = "mailchimp",
label = @Translation("MailChimp"),
category = @Translation("MailChimp"),
description = @Translation("Sends a form submission to a MailChimp list."),
cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\webform\Plugin\WebformHandlerBase implements WebformHandlerInterface uses WebformPluginSettingsTrait
- class \Drupal\webform_mailchimp\Plugin\WebformHandler\WebformMailChimpHandler
- class \Drupal\webform\Plugin\WebformHandlerBase implements WebformHandlerInterface uses WebformPluginSettingsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformMailChimpHandler
File
- src/
Plugin/ WebformHandler/ WebformMailChimpHandler.php, line 26
Namespace
Drupal\webform_mailchimp\Plugin\WebformHandlerView source
class WebformMailChimpHandler extends WebformHandlerBase {
/**
* The token manager.
*
* @var \Drupal\webform\WebformTokenManagerInterface
*/
protected $token_manager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance
->setTokenManager($container
->get('webform.token_manager'));
return $instance;
}
/**
* Set Token Manager dependency
*/
protected function setTokenManager(WebformTokenManagerInterface $token_manager) {
$this->tokenManager = $token_manager;
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$fields = $this
->getWebform()
->getElementsInitializedAndFlattened();
$lists = mailchimp_get_lists();
$email_summary = $this->configuration['email'];
if (!empty($fields[$this->configuration['email']])) {
$email_summary = $fields[$this->configuration['email']]['#title'];
}
$email_summary = '<strong>' . $this
->t('Email') . ': </strong>' . $email_summary;
$list_summary = $this->configuration['list'];
if (!empty($lists[$this->configuration['list']])) {
$list_summary = $lists[$this->configuration['list']]->name;
}
$list_summary = '<strong>' . $this
->t('List') . ': </strong>' . $list_summary;
$markup = "{$email_summary}<br/>{$list_summary}";
return [
'#markup' => $markup,
];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'list' => [],
'email' => '',
'double_optin' => TRUE,
'mergevars' => '',
'interest_groups' => [],
'control' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$lists = mailchimp_get_lists();
$options = [];
foreach ($lists as $list) {
$options[$list->id] = $list->name;
}
$form['mailchimp'] = [
'#type' => 'fieldset',
'#title' => $this
->t('MailChimp settings'),
'#attributes' => [
'id' => 'webform-mailchimp-handler-settings',
],
];
$form['mailchimp']['update'] = [
'#type' => 'submit',
'#value' => $this
->t('Refresh lists & groups'),
'#ajax' => [
'callback' => [
$this,
'ajaxMailchimpListHandler',
],
'wrapper' => 'webform-mailchimp-handler-settings',
],
'#submit' => [
[
get_class($this),
'maichimpUpdateConfigSubmit',
],
],
];
$form['mailchimp']['list'] = [
'#type' => 'webform_select_other',
'#title' => $this
->t('List'),
'#required' => TRUE,
'#empty_option' => $this
->t('- Select an option -'),
'#default_value' => $this->configuration['list'],
'#options' => $options,
'#ajax' => [
'callback' => [
$this,
'ajaxMailchimpListHandler',
],
'wrapper' => 'webform-mailchimp-handler-settings',
],
'#description' => $this
->t('Select the list you want to send this submission to. Alternatively, you can also use the Other field for token replacement.'),
];
$fields = $this
->getWebform()
->getElementsInitializedAndFlattened();
$options = [];
foreach ($fields as $field_name => $field) {
if (in_array($field['#type'], [
'email',
'webform_email_confirm',
])) {
$options[$field_name] = $field['#title'];
}
}
$default_value = $this->configuration['email'];
if (empty($this->configuration['email']) && count($options) == 1) {
$default_value = key($options);
}
$form['mailchimp']['email'] = [
'#type' => 'webform_select_other',
'#title' => $this
->t('Email field'),
'#required' => TRUE,
'#default_value' => $default_value,
'#options' => $options,
'#empty_option' => $this
->t('- Select an option -'),
'#description' => $this
->t('Select the email element you want to use for subscribing to the mailchimp list specified above. Alternatively, you can also use the Other field for token replacement.'),
];
$options = [];
foreach ($fields as $field_name => $field) {
if (in_array($field['#type'], [
'checkbox',
'webform_toggle',
])) {
$options[$field_name] = $field['#title'];
}
}
$form['mailchimp']['control'] = [
'#type' => 'select',
'#title' => $this
->t('Control field'),
'#empty_option' => $this
->t('- Select an option -'),
'#default_value' => $this->configuration['control'],
'#options' => $options,
'#description' => $this
->t('DEPRECATED: Use Webform\'s core conditions tab instead.'),
];
$form['mailchimp']['mergevars'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Merge vars'),
'#default_value' => $this->configuration['mergevars'],
'#description' => $this
->t('You can map additional fields from your webform to fields in your MailChimp list, one per line. An example might be FNAME: [webform_submission:values:first_name]. You may use tokens.'),
];
$form['mailchimp']['interest_groups'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Interest groups'),
'#description' => $this
->t('Displays interest groups for the selected list. Visit <a href="@url">Getting Started with Groups</a> for more information.', [
'@url' => 'https://kb.mailchimp.com/lists/groups/getting-started-with-groups',
]),
];
// Get selected interest group. Fallback to the saved one.
$list_id = $form_state
->getValue([
'mailchimp',
'list',
], $this->configuration['list']);
if ($list_id) {
$list = mailchimp_get_list($list_id);
if (!empty($list->intgroups)) {
$groups_default = $this->configuration['interest_groups'];
if (empty($groups_default)) {
$groups_default = [];
}
$form['mailchimp']['interest_groups'] += mailchimp_interest_groups_form_elements($list, $groups_default, NULL, 'admin');
}
}
$form['mailchimp']['double_optin'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Double opt-in'),
'#default_value' => $this->configuration['double_optin'],
];
$form['mailchimp']['token_tree_link'] = $this->tokenManager
->buildTreeLink();
return $form;
}
/**
* Ajax callback to update Webform Mailchimp settings.
*/
public static function ajaxMailchimpListHandler(array $form, FormStateInterface $form_state) {
return $form['settings']['mailchimp'];
}
/**
* Submit callback for the refresh button.
*/
public function maichimpUpdateConfigSubmit(array $form, FormStateInterface $form_state) {
// Trigger list and group category refetch by deleting lists cache.
$cache = \Drupal::cache('mailchimp');
$cache
->delete('lists');
$form_state
->setRebuild();
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValues();
foreach ($this->configuration as $name => $value) {
if (isset($values['mailchimp'][$name])) {
// Filter out unset interest group ids so mailchimp_subscribe_process()
// doesn't subscribe all groups.
if ($name == 'interest_groups') {
if (!empty($values['mailchimp'][$name])) {
$filtered_groups = [];
foreach ($values['mailchimp'][$name] as $group_id => $interest_group) {
if ($group_subcriptions = array_filter($interest_group)) {
$filtered_groups[$group_id] = $group_subcriptions;
}
}
$this->configuration[$name] = $filtered_groups;
}
}
else {
$this->configuration[$name] = $values['mailchimp'][$name];
}
}
}
}
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
// If update, do nothing
if ($update) {
return;
}
$fields = $webform_submission
->toArray(TRUE);
// If there's a checkbox configured, check for its value
if (!empty($this->configuration['control']) && empty($fields['data'][$this->configuration['control']])) {
return;
}
$configuration = $this->tokenManager
->replace($this->configuration, $webform_submission);
// Email could be a webform element or a string/token.
if (!empty($fields['data'][$configuration['email']])) {
$email = $fields['data'][$configuration['email']];
}
else {
$email = $configuration['email'];
}
$mergevars = Yaml::decode($configuration['mergevars']) ?? [];
// Allow other modules to alter the merge vars.
// @see hook_mailchimp_lists_mergevars_alter().
$entity_type = 'webform_submission';
\Drupal::moduleHandler()
->alter('mailchimp_lists_mergevars', $mergevars, $webform_submission, $entity_type);
\Drupal::moduleHandler()
->alter('webform_mailchimp_lists_mergevars', $mergevars, $webform_submission, $this);
$handler_link = Link::createFromRoute(t('Edit handler'), 'entity.webform.handler.edit_form', [
'webform' => $this
->getWebform()
->id(),
'webform_handler' => $this
->getHandlerId(),
])
->toString();
$submission_link = $webform_submission
->toLink($this
->t('Edit'), 'edit-form')
->toString();
$context = [
'link' => $submission_link . ' / ' . $handler_link,
'webform_submission' => $webform_submission,
'handler_id' => $this
->getHandlerId(),
];
if (!empty($configuration['list']) && !empty($email)) {
$member_data = mailchimp_get_memberinfo($configuration['list'], $email, TRUE);
// If the user is already subscribed, do not set it back to pending
$double_optin = $configuration['double_optin'];
if (!empty($member_data->status) && $member_data->status == MailchimpLists::MEMBER_STATUS_SUBSCRIBED) {
$double_optin = FALSE;
}
mailchimp_subscribe($configuration['list'], $email, array_filter($mergevars, 'strlen'), $configuration['interest_groups'], $double_optin);
}
else {
if (empty($configuration['list'])) {
\Drupal::logger('webform_submission')
->warning('No mailchimp list was provided to the handler.', $context);
}
if (empty($email)) {
\Drupal::logger('webform_submission')
->warning('No email address was provided to the handler.', $context);
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
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. | |
WebformHandlerBase:: |
protected | property | The webform handler's conditions. | |
WebformHandlerBase:: |
protected | property | The webform handler's conditions result cache. | |
WebformHandlerBase:: |
protected | property | The webform submission (server-side) conditions (#states) validator. | |
WebformHandlerBase:: |
protected | property | The configuration factory. | 1 |
WebformHandlerBase:: |
protected | property | The webform handler ID. | |
WebformHandlerBase:: |
protected | property | The webform handler label. | |
WebformHandlerBase:: |
protected | property | The logger factory. | |
WebformHandlerBase:: |
protected | property | The webform variant notes. | |
WebformHandlerBase:: |
protected | property | The webform handler status. | |
WebformHandlerBase:: |
protected | property | The webform submission storage. | |
WebformHandlerBase:: |
protected | property | The webform token manager. | 6 |
WebformHandlerBase:: |
protected | property | The webform. | |
WebformHandlerBase:: |
protected | property | The webform submission. | |
WebformHandlerBase:: |
protected | property | The weight of the webform handler. | |
WebformHandlerBase:: |
public | function |
Controls entity operation access to webform submission. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Controls entity operation access to webform submission element. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Alter webform element. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Alter webform submission webform elements. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Alter webform submission form. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
protected | function | Apply submitted form state to configuration. | |
WebformHandlerBase:: |
protected | function | Build token tree element. | 2 |
WebformHandlerBase:: |
public | function |
Returns the webform handler cardinality settings. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Check handler conditions against a webform submission. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Confirm webform submission form. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a element after it has been created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been created and added to webform. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a element after it has been deleted. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been removed. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Returns the webform handler description. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Disables the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Validate form that should have tokens in it. | |
WebformHandlerBase:: |
public | function |
Enables the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the conditions the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the unique ID representing the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the label of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Get webform or webform_submission logger. | |
WebformHandlerBase:: |
public | function |
Returns notes of the webform variant. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get configuration form's off-canvas width. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Returns the status of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get the webform that this handler is attached to. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get the webform submission that this handler is handling. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the weight of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Determine if the webform handler requires anonymous submission tracking. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Determine if this handle is applicable to the webform. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler disabled indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler enabled indicator. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Checks if the handler is excluded via webform.settings. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform submission is optional indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform submission is required indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler label. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Log a webform handler's submission operation. | |
WebformHandlerBase:: |
public | function |
Alter/override a webform submission webform settings. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission after it is created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on deleted a webform submission before the delete hook is invoked. Overrides WebformHandlerInterface:: |
4 |
WebformHandlerBase:: |
public | function |
Acts on loaded webform submission. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on webform submissions after they are purged. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Changes the values of an entity before it is created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission before they are deleted and before hooks are invoked. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on an webform submission about to be shown on a webform submission form. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Prepares variables for webform confirmation templates. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on webform submissions before they are purged. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission before the presave hook is invoked. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
protected | function | Replace tokens in text with no render context. | |
WebformHandlerBase:: |
public | function |
Sets the conditions for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Sets the id for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the label for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set notes for this webform variant. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Set configuration settings parents. | |
WebformHandlerBase:: |
protected | function | Set configuration settings parents. | |
WebformHandlerBase:: |
public | function |
Sets the status for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set the webform that this is handler is attached to. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set the webform submission that this handler is handling. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the weight for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Submit webform submission form. Overrides WebformHandlerInterface:: |
4 |
WebformHandlerBase:: |
public | function |
Determine if webform handler supports conditions. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Determine if webform handler supports tokens. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Acts on a element after it has been updated. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been updated. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Validate webform submission form. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Constructs a WebformHandlerBase object. Overrides PluginBase:: |
7 |
WebformHandlerInterface:: |
constant | Value indicating a single plugin instances are permitted. | ||
WebformHandlerInterface:: |
constant | Value indicating unlimited plugin instances are permitted. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions are not processed (i.e. email or saved) by the handler. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions are processed (i.e. email or saved) by the handler. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions do not have to be stored in the database. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions must be stored in the database. | ||
WebformMailChimpHandler:: |
protected | property | The token manager. | |
WebformMailChimpHandler:: |
public static | function | Ajax callback to update Webform Mailchimp settings. | |
WebformMailChimpHandler:: |
public | function |
Form constructor. Overrides WebformHandlerBase:: |
|
WebformMailChimpHandler:: |
public static | function |
Creates an instance of the plugin. Overrides WebformHandlerBase:: |
|
WebformMailChimpHandler:: |
public | function |
Gets default configuration for this plugin. Overrides WebformHandlerBase:: |
|
WebformMailChimpHandler:: |
public | function |
Returns a render array summarizing the configuration of the webform handler. Overrides WebformHandlerBase:: |
|
WebformMailChimpHandler:: |
public | function | Submit callback for the refresh button. | |
WebformMailChimpHandler:: |
public | function |
Acts on a saved webform submission before the insert or update hook is invoked. Overrides WebformHandlerBase:: |
|
WebformMailChimpHandler:: |
protected | function | Set Token Manager dependency | |
WebformMailChimpHandler:: |
public | function |
Form submission handler. Overrides WebformHandlerBase:: |
|
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function |