Hello,

I have the following:

Node Type: blah
-has title field
-has nodereference field

I would like to, as a way to allow users to add many nodes of this type at once, let users do the following on the node add form:

Title: cat, dog, rabbit, chicken
Nodereference: animal (this is a node of a different type)

Resulting in 4 nodes:

Title: cat
Nodereference: animal

Title: dog
Nodereference: animal

Title: rabbit
Nodereference: animal

Title: chicken
Nodereference: animal

Can I do this with a small amount of custom code in a template file somewhere? Is there already a module that does this? Someone suggested Rules but that seems rather bulky just for what seems to be a minor thing.

Thanks!

Comments

msielski’s picture

This may not work, but it's an idea you could try...

My approach would be this: the user is creating a node with "a,b,c,d" in the title and a node reference field that points to X. We want 4 nodes with titles a, b, c, and d all with node references that point to X. Let's try using hook_nodeapi to perform the creation of these extra nodes when this original node is getting saved. Also, lets let that original node get saved, but it will only be creating the first item in the series - a in this case. We'll have some PHP code create nodes b,c,d as copies.


function mymodulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // mynodetype below is the system name of the content type you want this to work with

  if (($node->type == 'mynodetype') && ($op == 'prepare')) {

    $titles_array = explode(',', $node->title);

    $title_index = 1;
    foreach($titles_array as $current_title) {

      if ($title_index == 1) {
        $node->title = $current_title;  // this node will represent the first term in the title, so set it here
      } else {  // we need to make a copy

        $clone = $node;  // need to test this, i am not clear on references with assignment
        $clone->is_new = true;
        $clone->title = $current_title;
        unset($clone->nid);
        unset($clone->vid);
        drupal_save($clone);

      }      
      $title_index++;
    }

  }

}

Definitely test and clean it up, but hopefully this gets you started.

msielski’s picture