You are here

function commons_trusted_contacts_node_presave in Drupal Commons 7.3

Implements hook_node_presave().

Override the group fields.

File

modules/commons/commons_trusted_contacts/commons_trusted_contacts.module, line 1081

Code

function commons_trusted_contacts_node_presave($node) {
  if (!module_exists('og_access') || og_is_group('node', $node)) {
    return;
  }

  // Act only on Commons Groups group content entities.
  $group_content_entity_types = commons_groups_get_group_content_entity_types();
  if (!isset($group_content_entity_types['node'][$node->type])) {
    return;
  }

  // We're posting from the short form on the site-wide homepage.
  if (isset($node->form_state['values']['group_audience_type'])) {
    $group_audience_type = $node->form_state['values']['group_audience_type'];
  }

  // We're posting from the full node creation form.
  if (isset($node->group_audience_type)) {
    $group_audience_type = $node->group_audience_type;
  }
  $wrapper = entity_metadata_wrapper('node', $node);

  // The content is submitted to the author's trusted contacts group, rather
  // than to specific groups.
  if (isset($group_audience_type) && $group_audience_type == 'private') {
    $wrapper->og_user_group_ref
      ->set(array(
      $node->uid,
    ));
    $wrapper->group_content_access
      ->set(OG_CONTENT_ACCESS_PRIVATE);

    // Remove any existing selected groups.
    $wrapper->{OG_AUDIENCE_FIELD}
      ->set(array());
  }
  else {
    if (!empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE])) {

      // Extract group IDs from selection.
      $group_ids = array();
      foreach ($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE] as $group) {
        $group_ids[] = $group['target_id'];
      }
      $wrapper->{OG_AUDIENCE_FIELD}
        ->set($group_ids);
      $wrapper->og_user_group_ref
        ->set(array());
    }
  }
}