background image
Eric'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);      
  }
}
?>

You can also use the

You can also use the auto_nodetitles module for this, it allows you to automatically generate node titles with tokens taken from the fields, specifyable per content type.

hey

so where do i put this code?

Eric's picture

module

Most of the code on my blog goes in a module or theme. In this case, a module. Please see: http://drupal.org/developing/modules

Cheers,
Eric

hello

very cool realy thanks....evden eve nakliyat
evden eve nakliyat

Yes. Thank u for information.

Yes. Thank u for information. I can try it.
evden eve nakliyat