You are here

function message_subscribe_get_basic_context in Message Subscribe 7

Get context from entity type.

This is a naive implementation, which extracts context from an entity. For example, given a node we extract the node author and related taxonomy terms.

Parameters

$entity_type: The entity type.

$entity: The entity object.

$subscribe_options: Optional; The options array to pass to message_subscribe_get_basic_context().

$context: Optional; The context array.

Return value

Array keyed with the entity type and array of entity IDs as the value.

3 calls to message_subscribe_get_basic_context()
MessageSubscribeContextTest::testGetBasicContext in ./message_subscribe.test
message_subscribe_get_subscribers in ./message_subscribe.module
Get a list of user IDs that need to recieve the message.
message_subscribe_send_message in ./message_subscribe.module
Process a message, and send to subscribed users.

File

./message_subscribe.module, line 315
Subscribe API for the Message and Message notify modules.

Code

function message_subscribe_get_basic_context($entity_type, $entity, $subscribe_options = array(), $context = array()) {
  if (!$context) {
    list($id) = entity_extract_ids($entity_type, $entity);
    $context[$entity_type][$id] = $id;
  }
  if (!empty($subscribe_options['skip context'])) {
    return $context;
  }
  $context += array(
    'node' => array(),
    'user' => array(),
    'taxonomy_term' => array(),
  );

  // Default context for comments.
  if ($entity_type == 'comment') {
    $context['node'][$entity->nid] = $entity->nid;
    $context['user'][$entity->uid] = $entity->uid;
  }
  if (empty($context['node'])) {
    return $context;
  }
  $nodes = node_load_multiple($context['node']);
  if (module_exists('og')) {

    // Iterate over existing nodes to extract the related groups.
    foreach ($nodes as $node) {
      foreach (og_get_entity_groups('node', $node) as $group_type => $gids) {
        foreach ($gids as $gid) {
          $context[$group_type][$gid] = $gid;
        }
      }
    }
  }
  $nodes = node_load_multiple($context['node']);
  foreach ($nodes as $node) {
    $context['user'][$node->uid] = $node->uid;
    if (module_exists('taxonomy')) {
      $context['taxonomy_term'] = !empty($context['taxonomy_term']) ? $context['taxonomy_term'] : array();

      // Iterate over all taxonomy term reference fields, or entity-reference
      // fields that reference terms.
      foreach (array_keys(field_info_instances('node', $node->type)) as $field_name) {
        $field = field_info_field($field_name);
        if ($field['type'] == 'taxonomy_term_reference' || $field['type'] == 'entityreference' && $field['settings']['target_type'] == 'taxonomy_term') {
          $wrapper = entity_metadata_wrapper('node', $node);
          if ($tids = $wrapper->{$field_name}
            ->value(array(
            'identifier' => TRUE,
          ))) {
            $tids = $field['cardinality'] == 1 ? array(
              $tids,
            ) : $tids;
            foreach ($tids as $tid) {
              $context['taxonomy_term'][$tid] = $tid;
            }
          }
        }
      }
    }
  }
  return $context;
}