I have content types where I want some types of users to post at the site global level. When I try to add one of these types, my group options are only:
* Post to specific groups
* Post to contacts (private)

How can I make a post without having to assign to a group? Checking "Post to specific groups " and leaving the group blank does not work. I don't want to remove these fields for this type of user, because they may want to post to a group or trusted contacts. How would you recommend doing this? Or is it a bug?

I have tried to make a patch to fix this, but it does not recognize the change. (the git repo setup has changed?). I had to add this to my theme's template.php (the group_audience_type is not available when trying to alter in a module).

  if (isset($form['group_audience_type'])) {
    $form['group_audience_type']['#options']['public'] = 'Public';
    $form['group_audience_type']['#default_value'] = 'public';
  }

Then in commons_trusted_contacts.module - line 943, could you change the else to:
else if ($group_audience_type == 'custom') {
Otherwise I get an error on $wrapper->{OG_AUDIENCE_FIELD}->set($group_ids);

Comments

RobKoberg’s picture

Or could we change this test:

  if (empty($form[OG_AUDIENCE_FIELD])) {
    $form['group_audience_type']['#options']['custom'] = t('Post site-wide');
  }

to be based on a custom permission. The could be something like:
"create content site-side"

  if (empty($form[OG_AUDIENCE_FIELD]) || user_access('create content site-side')) {
    $form['group_audience_type']['#options']['public'] = t('Post site-wide');
  }
RobKoberg’s picture

Title: Content types with group audience, but posted without a group context » With og_access enabled, how to post site-wide?

re-titling

RobKoberg’s picture

Category: feature » bug
Priority: Normal » Critical

I don't think the test in commons_trusted_contacts.module and the commons_trusted_contacts_form_add_privacy_toggle function is working correctly. When trying to do a similar test in my template.php form_alter there was never a default value at $form[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'] or $form['og_user_group_ref'][LANGUAGE_NONE][0]['default']['#default_value'] even though the value is set correctly in the DB.

This seems to be a bug now.

I had to do it from the node attached to the form to get correct values:

  if (isset($form['group_audience_type'])) {
    global $user;
    if (user_access('bypass node access') || in_array('partner', $user->roles)) {
      $default_value = 'public';
      if (!empty($form['#node']->{OG_AUDIENCE_FIELD})) {
        $default_value = 'custom';
      } else if (!empty($form['#node']->og_user_group_ref)) {
        $default_value = 'private';
      }
      $form['group_audience_type']['#options']['public'] = 'Post site-wide';
      $form['group_audience_type']['#default_value'] = $default_value;
    }
  }

Also can you put in the test to ensure we can override the either or behavior with og_access enabled of having to post to either private contacts or to a group (else if ($group_audience_type == 'custom') {). This should not affect the behavior of either having og_access enabled or not.

/**
 * Implements hook_node_presave().
 *
 * Override the group fields.
 */
function commons_trusted_contacts_node_presave($node) {
  if (!module_exists('og_access')) {
    return;
  }

  if (!empty($node->partial_node_form)) {
    $group_audience_type = $node->form_state['values']['group_audience_type'];
  }

  else {
    $group_audience_type = $node->group_audience_type;
  }

  $wrapper = entity_metadata_wrapper('node', $node);

  // The content is submitted either to the private group or to a selected
  // group.
  if ($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 ($group_audience_type == 'custom') {
    // 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());
  }
}
RobKoberg’s picture

Still having problems with this. No matter what I try to do to add a public option, it always saves as 'private' for trusted contacts. That is, I cannot seem to save a group content node without some group context. We were able to do this until recently, right?

This is critical for us.

Is there somewhere else that is forcing 'private'. In debugging, I am seeing the value of $group_audience_type is set to 'public' in the commons_trusted_contacts_node_presave function, so it would seem that it is not getting set in the commons_trusted_contacts_node_presave function. Where else could this be happening?

Here is my submit handler:

function mash_ensure_correct_group_access($form, $form_state) {
  if ($form_state['input']['group_audience_type'] == 'public') {
    $form_state['values'][OG_AUDIENCE_FIELD][LANGUAGE_NONE] = array();
    $form_state['values']['og_user_group_ref'][LANGUAGE_NONE] = array();

    $form_state['build_info']['args'][0]->{OG_AUDIENCE_FIELD} = array();
    $form_state['build_info']['args'][0]->og_user_group_ref = array();

    $form_state['complete_form'][OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['admin']['#default_value'] = '';
    $form_state['complete_form'][OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['admin']['#value'] = '';
    $form_state['complete_form']['og_user_group_ref'][LANGUAGE_NONE][0]['admin']['#default_value'] = '';
    $form_state['complete_form']['og_user_group_ref'][LANGUAGE_NONE][0]['admin']['#value'] = '';

    $form[OG_AUDIENCE_FIELD][LANGUAGE_NONE] = array();
    $form['og_user_group_ref'][LANGUAGE_NONE] = array();
    $form['group_audience_type']['#default_value'] = 'public';

    $wrapper = entity_metadata_wrapper('node', $form['#node']);
    $wrapper->{OG_AUDIENCE_FIELD}->set(array());
    $wrapper->og_user_group_ref->set(array());
    $form['#node']->{OG_AUDIENCE_FIELD} = array();
    $form['#node']->og_user_group_ref = array();
    $form['#node']->group_audience_type = 'public';
    $wrapper->save();
  }
}
RobKoberg’s picture

Using the hook_node_update and insert in a module (does not get called in the theme's template.php??). dpm'ing the node, I can see I have empty arrays for OG_AUDIENCE_FIELD and og_user_group_ref. Any ideas how to fix?

/**
 * Implements hook_node_update().
 */
function mash_node_update($node) {
  if (isset($node->group_audience_type) && $node->group_audience_type == 'public') {
    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper->{OG_AUDIENCE_FIELD}->set(array());
    $wrapper->og_user_group_ref->set(array());
dpm($node, 'mash_node_update $node');
  }
}

/**
 * Implements hook_node_insert().
 */
function mash_node_insert($node) {
  if (isset($node->group_audience_type) && $node->group_audience_type == 'public') {
    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper->{OG_AUDIENCE_FIELD}->set(array());
    $wrapper->og_user_group_ref->set(array());
    $node->{OG_AUDIENCE_FIELD} = array();
    $node->og_user_group_ref = array();
  }
}
ezra-g’s picture

Title: With og_access enabled, how to post site-wide? » Support site-wide posting outside of groups as a configuration option
Category: bug » feature
Priority: Critical » Major

Retitling as a feature request, since what's described here isn't part of the currently intended functionality in Commons.

@RobKoberg, are you able to file your proposed code changes above as a patch so that we can review it?

ezra-g’s picture

Status: Active » Closed (duplicate)
Leopold-2’s picture

important feature - should be implemented!