Hiya,

I'm writing a custom module so that when an anonymous user fills out a webform subscription form, he/she can be subscribed to a specified campaign monitor list.

I've got all the information contained in variables ready to be used with the emf_campaign_monitor_api_subscribe function ie:

    $email = $submission->data[$mapping['email_address_no_events']]['value'][0];
	$fields = array('Name' => $submission->data[$mapping['name']]['value'][0], 'Location' => $submission->data[$mapping['location']]['value'][0]);
	$lid = "5DKDS12CA4B70AE";
	emf_campaign_monitor_api_subscribe($email, $fields, $lid);
	drupal_set_message(t('Subscribed'));

But I can't figure out how to get this function firing and actually working! So far it just sits there dead in the water and I can't find any documentation or posts on this matter.

Can anyone give me a pointer or two?
Thanks

Comments

davyvdb’s picture

You shouldn't use the cm functions directly, rather use the emf wrapper functions. This will make your implementation work with all plugins.

To subscribe someone, use emf_db_save_request

This will queue a request. Run cron and your subscription should be done.

hfidge’s picture

Ahh of course - great thank you very much. If anyone is interested here's my module code to pick up the submission out of the webform and subscribe the user.

/**
 * Implementation of hook_webform_component_mapping
 * Used to parse the webform output into an easily accessible array
 */
function content_webform_component_mapping($node) {
  $mapping = array();
  $components = $node->webform['components'];
  foreach ($components as $i => $component) {
    $key = $component['form_key'];
    $mapping[$key] = $i;
  }
  return $mapping;
}


/**
 * Implementation of hook_webform_submission_insert
 * Used to interrupt the submission of the no-event form and write CSV
 */
function content_webform_submission_insert($node, $submission) {
  if ($node->nid == 128) {
    /**
    * Call the mapping function to parse the webform array and output the variables
    */
    $mapping = content_webform_component_mapping($node);
    /**
    * Write the CSV, including the human name for the form, selected from the node title.
    */
    $sql = "SELECT title FROM {node} WHERE nid = $node->nid";
    $result = db_query($sql);
	$nodetitle = str_replace(" ","-",db_result($result));
	$uniqueidentifier = substr($nodetitle,0,4) . $node->nid . $submission->sid . "|";
	$linereturn = "";
    $myFile = "files/Form-" . $nodetitle . ".txt";
    $fh = fopen($myFile, 'a') or die($write_error = 1);
	fwrite($fh, $uniqueidentifier);
	foreach($mapping as $key => $value){
      fwrite($fh, $submission->data[$mapping[$key]]['value'][0] . "|");
    }
	fwrite($fh, $linereturn."\r\n");
    fclose($fh);
    /**
     * Subscribe a user to a list.
     *
     * @param $email
     *   String; E-mail address to subscribe
     * @param $fields
     *   Array; Array of custom field values. Key is field. Value is value for the field.
     * @param $lid
     *   String; List ID of the list to subscribe to.
     * @return
     *   Boolean; TRUE if user is subscribed. FALSE if not.
     */
    $mail = $submission->data[$mapping['email_address_no_events']]['value'][0];
	$fields = array('name' => $submission->data[$mapping['name']]['value'][0], 'Location' => $submission->data[$mapping['location']]['value'][0]);
	$lid = "13546546874SDSF3434304KFJ";
	$type = "subscribe";
    emf_db_save_request($type, $mail, $lid, $fields);
	//drupal_set_message(t('You have been subscribed to the mailing list. If you wish to unsubscribe please do so using the unsubscribe details in the next email you receive. All your details will then be removed from our email database.'));	
  }
}

davyvdb’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)
Issue tags: -hooks (duplicate), -campaign monitor, -emf

Automatically closed -- issue fixed for 2 weeks with no activity.