You are here

function party_simplenews_entity_update in Party 8.2

Implements hook_entity_update.

File

modules/party_simplenews/party_simplenews.module, line 303
Main module file for Party Simplenews integration

Code

function party_simplenews_entity_update($entity, $type) {

  // Get necessary entity info.
  $wrapper = entity_metadata_wrapper($type, $entity);

  // Get data sets.
  $data_sets = party_get_data_set_info();

  // Is this entity party of a data set?
  $data_set_name = FALSE;
  foreach ($data_sets as $name => $def) {
    if ($def['entity type'] == $type && $def['entity bundle'] == $wrapper
      ->getBundle()) {
      $data_set_name = $name;
      break;
    }
  }

  // If it's not part of a data set do nothing else
  if (!$data_set_name) {
    return;
  }

  // Iterate over email fields on this data set
  foreach (party_get_email_fields($data_set_name) as $field_name) {

    // Get the items for this field.
    $items = field_get_items($type, $entity, $field_name);
    foreach ($items as $delta => $item) {

      // Get the subscriber with the right party_field_ref. Note, at the moment the
      // party_field_ref has the attached entity id in it, so we don't need to
      // check the party_id.
      $ref = $data_set_name . '__' . $wrapper
        ->getIdentifier() . '__' . $field_name . '__' . $delta;
      $subscriber = party_simplenews_subscriber_load_by_pid(NULL, $ref);
      if ($subscriber) {
        $subscriber->mail = $item['email'];
        simplenews_subscriber_save($subscriber);
      }
    }
  }
}