0

does anyone know how to create node reference while creating node programmatically? I mean - using TITLE and TYPE, not nid.

I am creating nodes via python script and user is prompted for title of parent node. Nobody's gonna tell you NID in that case. But content type and part of title should be enough.

If there is no way of doing that in Drupal - I am gonna make my own function to fetch all nodes that fits criteria and go further if there's only one or prompt for choosing proper one if there's more than one..

I'll be happy if there's already something that handles my task.. :)

My current code with doing that by NID:

    $n = new stdClass();
    $n->type="project_release";
    node_object_prepare($n);
    $n->title = "Kaboom [".date(YmdHis)."]";
    $n->language = LANGUAGE_NONE;
    $n->uid = 1;
    $n->body[$n->language][0]['value']="12345";
    $n->body[$n->language][0]['summary']="123457890";
    $n->body[$n->language][0]['format']="filtered_html";

    $n->field_release_project[$n->language][0]['nid']=85;
    $n->field_project_release_notes[$n->language][0]['value']='AaAAAAAA...';

    $path = 'remotely_added_at_'.date('YmdHis');
    $n->path = array('alias'=>$path);
    if($n=node_submit($n)) {
        node_save($n);
        echo "</br>Node with nid ".$n->nid." saved\n";
    }

Cheers

2 Answers 2

1

The only option you have is querying the node table to fetch the node id and re-ask if there are multiple matches yourself.

1
  • that's what I thought after checking google few times.. thanks.
    – wtk
    Commented Oct 21, 2011 at 8:11
3

I don't think that you need to write your own function, just have a look at node_reference_autocomplete_value(), this functions does exactly what you need (match Node Title to nid)

3
  • thanks for a hint! any ideas how to get around the "form state" parameter? function looks like: function node_reference_autocomplete_value($element, $input = FALSE, $form_state) but i don't have form_state.. Some example of its usage would be highly appreciated ;) I don't like Drupal and its modules documentations..
    – wtk
    Commented Oct 20, 2011 at 14:09
  • @wtk, which documentation are you looking at? The link in the answer shows function node_reference_autocomplete_value($element, $edit = FALSE), without a $form_state argument. Commented Oct 20, 2011 at 14:52
  • maybe I have a newer version of Reference module? I go to mentioned file and this function looks as I pasted. What I see giving null as a third argument is fine, but I don't see any example of argument which we should pass to function. I managed to : $k = node_reference_autocomplete_value( array( '#default_value' => '85' ), FALSE, null ); print_r($k); which gives a title of node number 85, but I need a reverse of that ;) this is how the functions looks: wklej.org/id/611084 - which for me looks like searching for a NID, not a title?
    – wtk
    Commented Oct 20, 2011 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.