You are here

function message_example_update_message_status in Message 7

Same name and namespace in other branches
  1. 8 modules/message_example/message_example.module \message_example_update_message_status()

Update the "published" field in the message entity, when it changes in the related entity.

Parameters

$entity_type: The entity type (node or comment).

$entity: The entity object.

2 calls to message_example_update_message_status()
message_example_comment_update in message_example/message_example.module
Implements hook_comment_update().
message_example_node_update in message_example/message_example.module
Implements hook_node_update().

File

message_example/message_example.module, line 91

Code

function message_example_update_message_status($entity_type, $entity) {
  if ($entity->status == $entity->original->status) {

    // status didn't change.
    return;
  }
  list($id) = entity_extract_ids($entity_type, $entity);
  $field_name = 'field_' . $entity_type . '_ref';
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'message')
    ->fieldCondition($field_name, 'target_id', $id, '=')
    ->execute();
  if (empty($result['message'])) {
    return;
  }
  foreach (array_keys($result['message']) as $mid) {
    $wrapper = entity_metadata_wrapper('message', $mid);

    // If comment status changed, we still need to check the node as-well.
    if ($entity_type == 'comment') {
      $node = node_load($entity->nid);
      $status = intval($entity->status && $node->status);
    }
    else {

      // The entity is the node.
      $status = $entity->status;
    }
    if ($wrapper->field_published
      ->value() != $status) {

      // Status changed, so update the message entity.
      $wrapper->field_published
        ->set($status);
      $wrapper
        ->save();
    }
  }
}