Here's the scoop...

I need to have the user create a 'contact' node. Within the 'contact' node, the user has the option to associate the 'contact' with a 'company'. The 'company' field auto-completes for 'company' nodes that already exist, however, if the entered 'company' doesn't exist, I need to have it automatically create a new 'company' node, both in the node table and in the associated 'company' table.

My current thinking is to manually insert the 'company' into the node table during the contact_submit function, get the new nid, and then manually insert a record into the 'company' table. However, this doesn't seem to be the Drupal way of doing things.

Any recommendations on how best to achieve this in the Drupal way?

- Martin

Comments

steven jones’s picture

Indeed, instead of manually inserting the node, and grabbing the nid, you can progmatically insert the node using drupal_execute

// Create a new node
$node = array('type' => 'story'); 
$values['title'] = 'My node'; 
$values['body'] = 'This is the body text!'; 
$values['name'] = 'robo-user'; 
drupal_execute('story_node_form', $values, $node);

http://api.drupal.org/api/5/function/drupal_execute

It works very well.

---
Regards
Darthsteven
http://aella.co.uk

---
Regards
Steven
http://www.computerminds.co.uk

Beltanin’s picture

I knew there had to be a better way. Thank you.

mlncn’s picture

I'd very much appreciate a little more detail on this for the terminally slow?

This is a killer feature-- to be able to create a node, if necessary, while creating another node.

I'm not clear on how to implement this elegant looking suggestion-- in a custom-themed form, can it be made as a module?

My use case is similar– creating nodes for authors, if not already in existence, while creating an article node with nodereferenced authors. I was originally envisioning authors as a free-tagging taxonomy to allow this on-the-fly creation, but while attractive it seems difficult to associate additional information with the author term. (My hack is just to associate an author node type with its own term manually, and list it first in views of nodes by that author.)

I can see many, many applications for creating nodes that you don't know you need to reference until halfway through creating the original node, and would love to learn more about it. Thanks!

~ben

People Who Give a Damn :: http://pwgd.org/ :: Building the infrastructure of a network for everyone
Agaric Design Collective :: http://AgaricDesign.com/ :: Open Source Web Development

benjamin, Agaric