You are here

function oa_subspaces_form_node_form_alter in Open Atrium Subspaces 7.2

Implements hook_form_alter(). Hook into the Node Edit form

File

./oa_subspaces.module, line 23

Code

function oa_subspaces_form_node_form_alter(&$form, $form_state, $form_id) {
  $node = $form['#node'];
  if (isset($form[OA_PARENT_SPACE][LANGUAGE_NONE][0])) {
    $form['#validate'][] = 'oa_subspaces_form_node_form_validate';

    // make some shortcut pointers to make the rest of this code easier to read
    $parent_default =& $form[OA_PARENT_SPACE][LANGUAGE_NONE][0]['default'];
    $parent_admin =& $form[OA_PARENT_SPACE][LANGUAGE_NONE][0]['admin'];

    // clean up the edit form
    if (!empty($parent_admin)) {

      // Hide admin field but allow any values to persist.
      $parent_admin['#access'] = FALSE;
    }
    $parent_default['#title'] = t('Parents');
    $parent_default['#description'] = t('Inherit membership from the selected parents.');
    $type = $node->type;
    $gid = oa_core_get_space_context();
    $set_parent = FALSE;
    if (!empty($_GET[OA_PARENT_SPACE]) && is_numeric($_GET[OA_PARENT_SPACE])) {
      $gid = $_GET[OA_PARENT_SPACE];
      $set_parent = TRUE;
    }

    // Make sure user can create in this group.
    $gid = og_user_access('node', $gid, "create {$node->type} content") ? $gid : NULL;

    // See if the user could create this type without parent set.
    $admin_access = user_access('administer group') || node_access('create', $type);
    if (empty($node->nid)) {

      // If they are being granted create permission based on group permission
      // restrict the parent field to current group so don't have to deal with
      // changing values in form based on changed parent field.
      if (!$admin_access) {
        if (!$gid) {
          drupal_access_denied();
          die;
        }
        else {
          $set_parent = TRUE;
          $parent_default['#access'] = FALSE;
          $parent_default = $gid;
          drupal_set_title(t('Create @type in @title', array(
            '@type' => node_type_get_name($type),
            '@title' => $parent_default['#options'][$gid],
          )));
        }
      }
    }
    elseif (!$admin_access && !empty($form['field_oa_space_type'])) {
      $form['field_oa_space_type']['#access'] = FALSE;
      $form[OA_PARENT_SPACE]['#access'] = FALSE;
    }
    if (isset($form[OG_USER_INHERITANCE_FIELD][LANGUAGE_NONE]) && isset($form[OG_USER_INHERITANCE_PERMISSION_FIELD][LANGUAGE_NONE])) {
      $form[OG_USER_INHERITANCE_PERMISSION_FIELD][LANGUAGE_NONE]['#states'] = array(
        'visible' => array(
          ':input[name="og_user_inheritance[und]"]' => array(
            'value' => 1,
          ),
        ),
      );

      // This field acts odd when required but user doesn't have access, throws
      // a required warning when 0 is valid and works fine otherwise
      $form[OG_USER_INHERITANCE_PERMISSION_FIELD][LANGUAGE_NONE]['#required'] = FALSE;
    }

    // Fill in default value.
    if (empty($node->nid) && $set_parent && $gid && ($group = node_load($gid))) {
      $parent_default['#default_value'] = $group->title . ' (' . $group->nid . ')';
      $parent_default['#init'][$group->nid] = $parent_default['#default_value'];
    }
  }
}