You are here

function commons_groups_node_presave in Drupal Commons 7.3

Implements hook_node_presave().

When the node's group is private, force the group content to be private.

File

modules/commons/commons_groups/commons_groups.module, line 745

Code

function commons_groups_node_presave($node) {
  if (!module_exists('og_access')) {
    return;
  }
  $wrapper = entity_metadata_wrapper('node', $node);
  if (og_is_group('node', $node)) {

    // Determine whether the group is private according to the subscription
    // field.
    $private = $wrapper->field_og_subscribe_settings
      ->value() == 'invitation';
    $wrapper->{OG_ACCESS_FIELD}
      ->set((int) $private);
    return;
  }
  if (!og_is_group_content_type('node', $node->type)) {
    return;
  }

  // Check whether any of the groups are private.
  $private = FALSE;
  foreach (array_keys(og_get_group_audience_fields('node', $node->type)) as $field) {
    if (empty($node->{$field})) {
      continue;
    }
    foreach ($wrapper->{$field} as $group_wrapper) {
      if (empty($group_wrapper->field_og_access_default_value)) {
        continue;
      }
      if ($group_wrapper->field_og_access_default_value
        ->value() == TRUE) {

        // Once a private group was found, there's no need to continue.
        $private = TRUE;
        break 2;
      }
    }
  }
  if ($private) {
    $wrapper->{OG_CONTENT_ACCESS_FIELD}
      ->set(OG_CONTENT_ACCESS_PRIVATE);
  }
}