0

I'm syncing a Drupal 8 webform to populate a MailChimp lits via the MailChimp module. As well as capturing the first and last name I also want to capture the City and Province.

However when I submit I only seem to capture the email address in MailChimp.

In Drupal 8 I configured my form for each field as the following:

Email Field (this works)
Title: Email
Key: email
Type: Email

First Name (this does not transfer to MailChimp)
Title: First Name
key: fname
Type: Text field

Last Name (this does not transfer to MailChimp)
Title: Last Name
key: lname
Type: Text field

In MailChimp I set up in the List fields and |MERGE| tags the following:

First Name *|FNAME|*
Last Name *|LNAME|*

Has anyone else had issues with merging fields from Drupal 8 into MailChimp via the MailChimp module?

2

I've recently built a subscriber module with a small piece of custom code. I prefer working with webform handlers instead of the Mailchimp module. In a custom module add a folder src/Plugin/WebformHandler and create a file newsletterSubscription.php. Add the following code:

<?php
namespace Drupal\<mycustommodule>\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;

/**
 * Form submission handler.
 *
 * @WebformHandler(
 *   id = "newslettersubscription",
 *   label = @Translation("Newsletter subscription"),
 *   category = @Translation("Form Handler"),
 *   description = @Translation("Administers subscriptions in Mailchimp"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */

class newsletterSubscription extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  const MAILCHIMP_API_KEY = 'x'; // see https://mailchimp.com/help/about-api-keys
  const LIST_ID = 'x'; // see https://3by400.com/get-support/3by400-knowledgebase?view=kb&kbartid=6


  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

    $values = $webform_submission->getData();
    $email = strtolower($values['e_mail']);
    $first_name = $values['first_name'];
    $last_name = $values['last_name'];

    // The data to send to the API
    $postData = array(
      "email_address" => "$email",
      "status" => "subscribed",
      "merge_fields" => array(
        "FNAME" => "$first_name",
        "LNAME" => "$last_name"
      )
    );

    // Setup cURL
    // To get the correct dataserver, see the url of your mailchimp back-end, mine is https://us20.admin.mailchimp.com/account/api/
    $ch = curl_init('https://us20.api.mailchimp.com/3.0/lists/'.self::LIST_ID.'/members/');
    curl_setopt_array($ch, array(
      CURLOPT_POST => TRUE,
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_HTTPHEADER => array(
        'Authorization: apikey '.self::MAILCHIMP_API_KEY,
        'Content-Type: application/json'
      ),
      CURLOPT_POSTFIELDS => json_encode($postData)
    ));

    // Send the request
    $response = curl_exec($ch);
    $readable_response = json_decode($response);
    if($readable_response->status == 403) {
      \Drupal::logger('Mailchimp_subscriber')->error($readable_response->title.': '.$readable_response->detail .'. Raw values:'.print_r($values));
      \Drupal::messenger()->addError('Something went wrong. Please contact your webmaster.');
    }
    if($readable_response->status == 'subscribed') {
      \Drupal::messenger()->addStatus('You are now successfully subscribed.');
    }
    if($readable_response->status == 400) {
      if($readable_response->title == 'Member Exists') {
        \Drupal::messenger()->addWarning('You are already subscribed to this mailing list.');
      }
    }

    return true;
  }
}

Read more on how to enable and use the handler here: How to create a mailchimp subscription with drupal 8 webform and custom handlers (Video)

0
  1. Go to the settings of your webform
  2. Add handler on the Emails/Handlers tab
  3. Select your List and Email field you wish to use
  4. In the merge vars section you can link your webform fields with fields from your Mailchimp webform. For example:

    FNAME: [webform_submission:values:name]
    LNAME: [webform_submission:values:surname]
    
0

For me to get this to work, I needed to quote the tokens:

FNAME: '[webform_submission:values:name]'
LNAME: '[webform_submission:values:surname]'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.