'admin/settings/user_tab', 'title' => t('User Tab'), 'description' => t('Provides an extra tab in Drupals user profile and a generic node type called user profile that you can edit with CCK'), 'callback' => 'drupal_get_form', 'callback arguments' => array('user_tab_settings'), ); } else { if (variable_get('user_tab_profile', 0) && (arg(0) == 'user') && (is_numeric(arg(1)))) { $type = ('user_tab'); $nid = user_tab_for_user(arg(1)); if ($nid) { $node = node_load($nid); } else { $node = array(); $node['title'] = 'User Profile'; $node['type'] = $type; $node['uid'] = arg(1); $node['name'] = $user->name; $node['status'] = 1; $node = node_submit($node); node_save($node); } // this makes the tab, and gives you the option to rename it in the admin settings $items[] = array( 'path' => 'user/'.arg(1).'/user_tab/view', 'title' => variable_get('user_tab_name', t('user profile')), 'callback' => 'node_page_view', 'callback arguments' => array($node), 'type' => MENU_LOCAL_TASK, 'weight' => -4, ); // this makes the second tab for editing the user profile $items[] = array( 'path' => 'user/'.arg(1).'/user_tab/'.arg(1).'/edit/user_tab', 'title' => t('Edit your '). variable_get('user_tab_name', t('user profile')), 'callback' => 'drupal_get_form', 'callback arguments' => array($node->type .'_node_form', $node), 'access' => $user->uid == $node->uid, 'type' => MENU_LOCAL_TASK, 'weight' => -3, ); } elseif (variable_get('user_tab_profile', 0) && (arg(0) == 'node') && is_numeric(arg(1)) && !arg(2)) { // if we're about to visit a user_tab node page, but we've got "use user_tabs for profiles" selected, // redirect to the user page $node = node_load(arg(1)); if (user_access('access user profiles')&& $node->type == ('user_tab')) { drupal_goto('user/'. $node->uid); } } } return $items; } /** * Implementation of hook_form_alter * * Handle the * redirect to the user profile page for better usability */ function user_tab_form_alter($form_id, &$form) { if ($form_id == ('user_tab') ."_node_form" && arg(0) == 'user') { // we're editing the user_tab in the user area... be sure we end up here when we finish submission $account = user_load(array('uid' => $form['uid']['#value'])); $form['#redirect'] = 'user/'. $account->uid.'/user_tab'; if (user_access('administer nodes')) { $form['author']['name']['#default_value'] = $account->name; $form['author']['name']['#value'] = $account->name; $form['author']['name']['#disabled'] = 'disabled'; unset($form['author']['name']['#autocomplete_path']); $form['author']['name']['#description'] = t(' This field is disabled. You cannot alter the author of this entry from within the user area.'); } } } function user_tab_profile_alter(&$account, &$fields) { if (variable_get('user_tab_profile_takeover', 0) && $user_tab = user_tab_for_user($account->uid)) { $typename = node_get_types('name', 'user_tab'); foreach($fields as $key => $val) { if ($key != $typename) { unset($fields[$key]); } } $account->name = $user_tab->title; } } /** * Implementation of hook_node_info() * * - create default "user_tab" node type called user profile */ function user_tab_node_info() { // The default user_tab node type is simple so we'll just use node as the base. if (variable_get('user_tab_nodetype', 'user_tab') == 'user_tab') { return array('user_tab' => array( 'name' => t('user profile'), 'description' => t('Provides an basic node type called user profile that you can edit extend with CCK'), 'module' => 'node', 'title' => $account->name, 'has_body' => TRUE, 'custom' => TRUE, 'modified' => TRUE, 'locked' => FALSE, )); } } /** * Implementation of hook_nodeapi() * * - validate user_tab node form to make sure that the user doesn't already * have one */ function user_tab_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { if ($node->type != ('user_tab')) return; switch ($op) { case 'validate': // This user already has a user_tab node and this isn't it $account = user_load(array('name' => $node->name)); $nid = user_tab_for_user($account->uid); if ($nid && ($nid != $node->nid)) { form_set_error('name', t('You have already a @user_tab. Edit it here or assign this entry to another user.', array('@user_tab' => node_get_types('name', $node), '@link' => url('node/'.$nid.'/edit')))); } break; } } /** * Implentation hook_user() * * - add user_tab to main user profile page */ function user_tab_user($op, &$edit, &$user, $category = NULL) { if (!variable_get('user_tab_profile_takeover', 0)) return; switch ($op) { case 'view': if (!$nid = user_tab_for_user($user->uid)) return; if (!node_access('view', $node = node_load($nid))) return; return array(node_get_types('name', variable_get('user_tab_nodetype', 'user_tab')) => array('user_tab' => array('value' => node_view($node, FALSE, TRUE, FALSE)))); } } /** * Return node id of the user_tab for a given user * * FALSE if user has no user_tab node */ function user_tab_for_user($uid = NULL){ if (is_null($uid)){ global $user; $uid = $user->uid; } return db_result(db_query("SELECT nid FROM {node} WHERE uid = %d AND type = '%s'", $uid, 'user_tab')); } /** * Callback function for settings page * * Settings for * - use user_tab for profile * - use user_tab take over the core user profile */ function user_tab_settings() { $types = array(); foreach(node_get_types() as $key => $type) { $types[$key] = $type->name; } $form['user_tab_name'] = array( '#type' => 'textfield', '#size' => 20, '#title' => t('Rename user tab'), '#description' => t('Rename the tab in your user profiles.'), '#default_value' => variable_get('user_tab_name', t('user profile')), ); $form['user_tab_profile'] = array( '#type' => 'checkbox', '#title' => t('Use User Tab for user profiles'), '#description' => t('View the User Profile information on the user account page as an extra Tab this is the default Value.'), '#default_value' => variable_get('user_tab_profile', 0), ); $form['user_tab_profile_takeover'] = array( '#type' => 'checkbox', '#title' => t('Takeover profile.'), '#description' => t('Display nothing but the User Profile node on the user profile page.'), '#default_value' => variable_get('user_tab_profile_takeover', 0), ); return system_settings_form($form); }