tags:

views:

678

answers:

2

I am using auto node title which will generate the title of a node. However, this is not happening when I create a node using node_save. See below:

function save_contact($firstName, $lastName, $email, $showErrors = false) {

   global $user;

   $edit = array();
   $edit['type'] = 'contact';
   $edit['uid'] = $user->uid;
   $edit['name'] = $user->name;
   $edit['promote'] = 0;
   $edit['comment'] = 0;
   $edit['status'] = 1;

   $edit['field_contact_name'][0]['value'] = $firstName; // NOTE
   $edit['field_contact_surname'][0]['value'] = $lastName; // NOTE
   $edit['field_contact_email'][0]['email'] = $email; // NOTE
   $edit['title'] = $firstName.' '.$lastName; // NOTE

   node_invoke_nodeapi($edit, 'contact');
   node_validate($edit);
   $node = node_submit($edit);
   node_save($node);


}

save_contact("NAME", "SURNAME", "[email protected]");

When the node is generated, the title becomes: "[field_contact_name-formatted] [field_contact_surname-formatted] " instead of, "NAME SURNAME".

Any idea why? I'm guessing auto node title is not picking up that I've entered values, or that maybe the title generation happens before the point in time where I specify the values.

Any ideas?

+1  A: 

Line 74 of *auto_nodetitle.module*

return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title));

By that logic, have you included your contact content type in auto_nodetitle? If contact is only covered "optionally" then setting your title here will prevent the module from acting.

Grayside
I have set the contact to have an automatic node_title of [field_contact_name-formatted] [field_contact_surname-formatted]My question is, why does it actually make that the title, and not "NAME SURNAME". At what point do I have to set the title, to bypass auto node title?
RD
Or which fields must I set, for it to automatically set the title correctly?
RD
From peaking around it looked like the node title is set as part of the node_submit() process.
Grayside
+1  A: 

I fixed it like so:

  $edit['field_contact_name'] = array();
$edit['field_contact_name'][0]['value'] = 
    $edit['field_contact_name'][0]['safe'] = 
        $edit['field_contact_name'][0]['view'] = $firstName;

$edit['field_contact_surname'] = array();
$edit['field_contact_surname'][0]['value'] = 
    $edit['field_contact_surname'][0]['safe'] =
        $edit['field_contact_surname'][0]['view'] = $lastName;

$edit['field_contact_email'] = array();
$edit['field_contact_email'][0]['email'] =
    $edit['field_contact_email'][0]['safe'] = 
        $edit['field_contact_email'][0]['view'] = $email;

In other words, I had to also specifiy the safe and view fields.

RD

related questions