You are here

function party_entity_update in Party 8.2

Same name and namespace in other branches
  1. 7 party.module \party_entity_update()

Implements hook_entity_update($entity, $type);

Trigger rules and update party label if an attached entity has been saved.

File

./party.module, line 1518
Provides a generic CRM party entity.

Code

function party_entity_update($entity, $type) {
  if ($type == "party" && module_exists('rules')) {
    rules_invoke_event('party_update', $entity);
  }

  // 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;
  }

  // Find the party or parties this entity is attached to.
  $party_ids = db_select('party_attached_entity', 'pae')
    ->fields('pae', array(
    'pid',
  ))
    ->condition('data_set', $data_set_name)
    ->condition('eid', $wrapper
    ->getIdentifier())
    ->execute()
    ->fetchCol();

  // Update the label
  foreach ($party_ids as $pid) {
    $controller = entity_get_controller('party');
    $party = party_load($pid);
    $controller
      ->setLabel($party);
    $controller
      ->setPrimaryFields($party);
  }
}