I have a 'newsletter' node on my Drupal site.

In a submit handler, I save the data in the node to a campaign, via:

mailchimp_campaign_save_campaign()

My problem is, I need to save my node twice before my changes appear at the mailchimp end. (I am checking my mailchimp archive page at http://us7.campaign-archive2.com/?u=xxx to see if the changes arrive).

Any change I make at the drupal end consistently arrives at the mailchimp end, when I have saved my node for the second time.

I have tried clearing the theme registry cache and the mailchchimp campaign cache in code, it isn't giving me any success though.


cache_clear_all('theme_registry', 'cache', TRUE);
cache_clear_all('mailchimp_campaign_campaigns', 'cache');

Any clues about what might be happening here? Thanks!

CommentFileSizeAuthor
#1 mailchimp-debug.png81.05 KBmichaellenahan
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

michaellenahan’s picture

FileSize
81.05 KB

Debugging this right now, if I put a dpm() in mailchimp_campaign.module, line 309 (version 7.x-3.4)

  // Convert template to content by running through formatter.
  if (isset($template['html'])) {
    $content = mailchimp_campaign_render_template($template);
  }
  else {
    $content = array(
      'sections' => mailchimp_campaign_render_template($template),
    );
  }

dpm($content);

  // Test for valid list segment, if selected.
  if ($segment_options != NULL) {
    if (mailchimp_test_list_segment($options['list_id'], $segment_options['saved_segment_id']) === NULL) {
      return NULL;
    }
  }

This dpm($content); is giving me the old data from my node, not the latest field data.

On a subsequent save of my node, the $content variable is correct.

michaellenahan’s picture

Status: Active » Fixed

Wow. I had fun fixing this.

The node being used by mailchimp_campaign_save_campaign() was a stale one from cache.

My first thought was to move my code out of my submit handler, because I'm thinking that the node isn't saved yet when we are still submitting.

So I moved my code to hook_node_insert() and hook_node_update() instead, but I was still getting the same problem.

The fix was to clear the node cache, just before calling mailchimp_campaign_save_campaign(). Now mailchimp_campaign_save_campaign() sees the saved node, not the old stale cached node.

entity_get_controller('node')->resetCache(array($newsletter_nid));
$campaign_id = mailchimp_campaign_save_campaign($template_content, $options, $segment_options, $campaign_id);

In case it helps anyone out there, the fuller picture looks like this, and it's working for me now.

/**
 * Implements hook_node_insert().
 */
function f_mailchimp_node_insert($node) {
  if ($node->type == 'newsletter') {
    $nid = $node->nid;
    $title = $node->title;
    $campaign_id = NULL;
    _f_mailchimp_save_campaign($nid, $title, $campaign_id);
  }
}

/**
 * Implements hook_node_update().
 */
function f_mailchimp_node_update($node) {
  if ($node->type == 'newsletter') {
    $nid = $node->nid;
    $title = $node->title;
    if (isset($node->field_newsletter_campaign_id[LANGUAGE_NONE][0]['value'])) {
      $campaign_id = $node->field_newsletter_campaign_id[LANGUAGE_NONE][0]['value'];
    }
    else {
      $campaign_id = NULL;
    }
    $campaign_id = _f_mailchimp_save_campaign($nid, $title, $campaign_id);

    cache_clear_all('mailchimp_campaign_campaigns', 'cache');

    // http://anthonypower.com/notebook/assigning-node-field-values-hook-insert-and-update-right-way
    $node->original = isset($node->original) ? $node->original : NULL;
    $node->field_newsletter_campaign_id[$node->language][0]['value'] = $campaign_id;
    field_attach_update('node', $node);
  }
}

/**
 * Sends newsletter data to mailchimp_campaign_save_campaign().
 */
function _f_mailchimp_save_campaign($nid, $title, $campaign_id) {
  $newsletter_nid = $nid;
  $template_content = array(
    'newsletter' => array(
      'value' => '[mailchimp_campaign|entity_type=node|entity_id=' . $newsletter_nid . '|view_mode=mailchimp]',
      'format' => 'mailchimp_campaign',
    ),
  );

  $options = array(
    'title' => $title,
    'subject' => $title,
    'list_id' => 'xxx',
    'from_email' => 'info@xxx.xx',
    'from_name' => 'xxx',
    'template_id' => 'xxx',
  );

  $segment_options = NULL;
  entity_get_controller('node')->resetCache(array($newsletter_nid));
  $campaign_id = mailchimp_campaign_save_campaign($template_content, $options, $segment_options, $campaign_id);
  return $campaign_id;
}

Status: Fixed » Closed (fixed)

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