Search code examples
drupaldrupal-8drupal-webform

Problem with Webform Mail Chimp on Drupal 8.7.8


Recently we updated a website made with Drupal 8. The update was from core 8.6 to 8.7.8, at the same time all the modules were updated as well. Among those modules were Webform and Webform Mailchimp.

After that, some of the forms started showing a warning after the form is submitted. The e-mail is sent and the data of the form is saved.

strlen() expects parameter 1 to be string, array is given in Drupal\webform_mailchimp\Plugins\WebformHancler\WebformMailChimpHandler->postSave()

The full error details can be seen here: https://prnt.sc/pn76pv

I'm new to Drupal so I think I should not go in the source file and make changes myself. How can I fix this?


Solution

  • From the error message, we can see that strlen() receives an array instead of a string :

    strlen() expects parameter 1 to be string, array is given in [...] WebformMailChimpHandler->postSave() (line 320 of [...])

    At Line 320 of this module, we have a call to mailchimp_subscribe() :

    mailchimp_subscribe($configuration['list'], $email, array_filter($mergevars, 'strlen'), $configuration['interest_groups'], $double_optin);
    

    The error is caused by what is passed in as the third argument : array_filter($mergevars, 'strlen'), meaning $mergevars - that is expected to be an array of strings to be filtered by strlen - contains an array instead of a string at some point.

    How to fix $mergevars ?

    We can see it's loaded from a config, and then altered by modules that implement some alter hooks :

    $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);
    

    First, you may want to check if $configuration['mergevars'] is correctly set.

    Then, you can see what is happening and eventually fix $mergevars in your own hook_alter :

    function MODULE_mailchimp_lists_mergevars_alter(&$mergevars, &$webform_submission, &$entity_type) {
      # debug/fix $mergevars
    }
    
    function MODULE_webform_mailchimp_lists_mergevars_alter(&$mergevars, &$webform_submission, &$WebformMailChimpHandler) {
      # debug/fix $mergevars
    }
    

    The issue is likely caused by a wrong mergevars alteration in one of the alter hooks. Note variables are passed by reference.

    If you find that $mergevars actually is an array of string (is it seems all good) inside your module alter hook, it means that another contrib module acting after yours (@see module priority) is causing the issue.