background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

In a recent project I used the content profile module to improve flexibility in creating user profiles. This module replaces the regular profile creation with a CCK node type. One of the requested profile fields was job title, which is confusing since title is already a required field of a node. In most cases, I would use first and last name as the title, but in this case those fields were required to be separate. Since the node title did not fit into the list of requested fields, I decided to remove it from the form and dynamically generate a node title from the user's first and last name. Here's how this can be accomplished:

<?php
// create a form_alter hook to remove the field from the form:
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  if (
$form_id == 'profile_node_form') {
   
// remove title from view
   
$form['title']['#access'] = false;
  }
   
}

// create a nodeapi function to create the node title when the node is created
function MYMODULE_nodeapi(&$node, $op, $a3=NULL, $a4=NULL) {
  if (
$node->type =='profile' && $op=='insert') {
   
$node->title = $node->field_profile_name_first[0]['value'] . " " . $node->field_profile_name_last[0]['value'];
   
node_save($node);      
  }
}
?>

Syndicate content