You are here

function oa_messages_mail_alter in Open Atrium Core 7.2

Implements hook_mail_alter().

File

modules/oa_messages/oa_messages.module, line 609

Code

function oa_messages_mail_alter(&$message) {
  if ($message['module'] != 'message_notify') {
    return;
  }
  $message_entity = $message['params']['message_entity'];
  $user = oa_users_build_user_details($message_entity->user);
  $message['username'] = $user['realname'];
  $message['picture'] = $user['picture'];
  $node = isset($message_entity->field_oa_node_ref) ? node_load($message_entity->field_oa_node_ref[LANGUAGE_NONE][0]['target_id']) : NULL;
  if (!$node) {
    return;
  }
  $comment = isset($message_entity->field_oa_comment_ref) ? comment_load($message_entity->field_oa_comment_ref[LANGUAGE_NONE][0]['target_id']) : NULL;

  // Set the heading and footer for the later mail template theme.
  if (empty($message['title']) && !empty($message['body'][0])) {

    // Extract any existing H3 from the message text as the title.
    $body = $message['body'][0];
    if (preg_match('/<h3>(.+?)<\\/h3>/', $body, $matches, PREG_OFFSET_CAPTURE)) {
      $message['title'] = $matches[1][0];
      $message['body'][0] = drupal_substr($body, 0, $matches[0][1]) . drupal_substr($body, $matches[0][1] + strlen($matches[0][0]));

      // Append any related content fields
      $entity = !empty($comment) ? $comment : $node;
      $entity_type = !empty($comment) ? 'comment' : 'node';
      if (!empty($entity->field_oa_related)) {
        $paragraphs = field_view_field($entity_type, $entity, 'field_oa_related');
        $paragraphs['#label_display'] = 'hidden';
        $message['body'][] = drupal_render($paragraphs);
      }
    }
  }

  // Add a link to original content in the email footer.
  if (empty($message['footer'])) {
    $message['footer'] = l(t('View original' . ' ' . node_type_get_name($node)), 'node/' . $node->nid);
  }
}