You are here

function gnode_form_node_form_alter in Group 7

Implements hook_form_BASE_FORM_ID_alter().

Adds a Group vertical tab to the node form.

You can only select those groups that you can create nodes of this type in. It would not make sense if someone could move nodes to a group where he does not have creation rights.

See also

gnode_node_validate()

gnode_node_submit()

File

modules/gnode/gnode.module, line 312
Contains Group's implementation of the Node module hooks and forms.

Code

function gnode_form_node_form_alter(&$form, $form_state) {
  $node = $form_state['node'];
  $gid = !empty($node->group) ? $node->group : 0;
  $can_bypass_access = user_access('bypass group access');

  // If the user can't bypass group access and we already have a nid or gid, we
  // are either on a node edit form or a Group Node add form. In such a case,
  // we do not show anything.
  if (!$can_bypass_access) {

    // Fix the group if the node was already set to one.
    if (!empty($gid)) {
      $form['gid'] = array(
        '#type' => 'value',
        '#value' => $gid,
        '#parents' => array(
          'group_settings',
          'gid',
        ),
      );
      return;
    }
    elseif (!empty($node->nid)) {
      return;
    }
  }
  $global_node_create_access = gnode_global_node_create_access($node->type);
  $group_access_warning = t("<strong>By selecting a group, the node will inherit the group's access control</strong>.");

  // Otherwise add a vertical tab for group selection.
  $form['group_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Group settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'node-form-group-information',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'gnode') . '/misc/gnode.js',
      ),
    ),
    '#tree' => TRUE,
    '#weight' => -50,
  );

  // Show a select of all the groups the author can create the node in.
  if (!$can_bypass_access) {
    $options = array();
    foreach (group_load_multiple(gnode_group_node_create_gids($node->type)) as $group) {
      $options[$group->gid] = $group
        ->label() . " (GID: {$group->gid})";
    }
    $description = $global_node_create_access ? t('Optionally select the group to attach this node to.') : t('Because you are not allowed to create this content outside of a group, you are required to select one here.');
    $form['group_settings']['gid'] = array(
      '#type' => 'select',
      '#title' => t('Parent group'),
      '#description' => $description . '<br />' . $group_access_warning,
      '#options' => $options,
      '#required' => !$global_node_create_access,
      '#empty_value' => '',
    );
  }
  else {
    $group_name = '';
    if ($gid) {

      // Retrieve the default value for the autocomplete field.
      $title = group_load($gid)->title;
      $group_name = "{$title} (GID: {$gid})";
    }
    $description = $global_node_create_access ? t('Enter the name of the group to attach this node to. Leave blank for no group.') : t('Because you are not allowed to create this content outside of a group, you are required to enter the name of one here.');

    // There is a special use case where a user may edit global nodes, but
    // not create them and also may bypass group access. In such a case, he
    // could move an ungrouped node to a group, or leave it ungrouped.
    if (!empty($node->nid) && empty($gid) && !$global_node_create_access) {
      $description = t('This node is not attached to a group. In order to keep it that way, leave this field blank. Alternatively, you may enter the name of the group to attach this node to.');
      $description .= '<br />' . t('<strong>Warning:</strong> Because you are not allowed to create this content outside of a group, you will not be able to move it back to the sitewide scope.');
      $global_node_create_access = TRUE;
    }
    $form['group_settings']['group'] = array(
      '#type' => 'textfield',
      '#title' => t('Parent group'),
      '#autocomplete_path' => 'group/autocomplete',
      '#default_value' => $group_name,
      '#description' => $description . '<br />' . $group_access_warning,
      '#required' => !$global_node_create_access,
    );
  }
}