You are here

function _spaces_og_form_alter_node in Spaces 6

Same name and namespace in other branches
  1. 5.2 spaces_og.module \_spaces_og_form_alter_node()
  2. 6.3 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()
  3. 6.2 spaces_og/spaces_og.module \_spaces_og_form_alter_node()
  4. 7.3 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()
  5. 7 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()

Group-enabled node form_alter()

1 call to _spaces_og_form_alter_node()
spaces_og_form_alter in spaces_og/spaces_og.module

File

spaces_og/spaces_og.module, line 692

Code

function _spaces_og_form_alter_node(&$form, $form_state) {
  global $user;
  $space = spaces_get_space();

  // Retrieve the content type label
  $types = node_get_types();
  if (!empty($types['group'])) {
    $typename = $types['group']->name;
  }

  // Collect groups for which this feature is enabled
  $options = array(
    0 => '--' . t('Select a !typename', array(
      '!typename' => $typename,
    )) . '--',
  );
  $valid_groups = _spaces_og_group_options($form['#node']->type);
  $user_groups = array();
  foreach (og_get_subscriptions($user->uid) as $node) {
    if (!empty($valid_groups[$node['nid']])) {
      $user_groups[$node['nid']] = $node['title'];
    }
  }

  // Give users access to only their groups
  $options[t('My !typenames', array(
    '!typename' => $typename,
  ))] = $user_groups;

  // Give admins access to all group options
  if (user_access('administer organic groups')) {
    $options[t('All !typenames', array(
      '!typename' => $typename,
    ))] = array_diff_key($valid_groups, $user_groups);
  }

  // Only show the dialogue if we have at least 1 group to target
  if (count($options > 1)) {

    // Node preview handling
    if (!empty($form['#node']->spaces_og)) {
      $default_gid = $form['#node']->spaces_og;
    }
    else {
      if (is_array($form['#node']->og_groups) && count($form['#node']->og_groups)) {
        reset($form['#node']->og_groups);
        $default_gid = key($form['#node']->og_groups);
      }
      else {
        if ($space) {
          $default_gid = $space->sid;
        }
        else {
          $default_gid = 0;

          // The invalid group
        }
      }
    }
    $form['spaces_og'] = array(
      '#type' => 'fieldset',
      '#tree' => true,
      '#title' => $typename,
    );
    $message = $form['#node']->nid ? t('Please select a !typename to move this post to.', array(
      '!typename' => strtolower($typename),
    )) : t('Please select a !typename to add this post to.', array(
      '!typename' => strtolower($typename),
    ));
    $form['spaces_og']['gid'] = array(
      '#required' => TRUE,
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default_gid,
      '#description' => $message,
      '#element_validate' => array(
        'spaces_og_nodeform_validate',
      ),
    );
  }

  // Recurse into og_options hiding all of them.
  _spaces_og_make_hidden($form['og_nodeapi']);

  // We can only determine the privacy of this node if currently in
  // a group space. Otherwise, it will be determined by the feature
  // setting of the group targeted by the selector above.
  if ($space->type == 'og') {
    $form['spaces'] = array(
      '#title' => t('Privacy'),
      '#type' => 'fieldset',
      '#weight' => 100,
    );
    switch ($form['#node']->og_public) {
      case OG_VISIBLE_GROUPONLY:
        $form['spaces']['#description'] = t('A post of this type is always <strong>private</strong>. Only members of this !typename will see it.', array(
          '!typename' => strtolower($typename),
        ));
        break;
      case OG_VISIBLE_BOTH:
        $form['spaces']['#description'] = t('A post of this type is always <strong>public</strong>. All visitors will see it.');
        break;
    }
  }
}