Dynamically fill field when adding a new node I have been trying to search for an answer to this but I am not entirely sure what to ask.  I am using drupal 7.  What I am trying to accomplish is being able to create a campaign, and then allowing users to order a product within that campaign.

So currently I have a content type of campaign and a content type of order.  I create a new campagin and then un-authenticated users should be able to create new Order nodes, but what I am not sure how to do, is link the orders to the campaign. So http://www.domain.com/campaign/name would have a form on it that generates a new order node.

It seems like I need to have entity reference in the order content type, but how to I populate that field because I don't want the user to select which campaign, it should be automatic.  Hopefully that makes sense, the only way I have thought to accomplish this would be to parse the uri and try to get the campaign name then lookup the campaign nid.  Seems kind of hackish though.  Thanks!
I don't know a clever way to link, so I would probably use the NID from campaign as a reference. 

I'd create a integer field in order which would store the NID from the campaign on creation. i.e. The custom form would create a node of type order and automatically fill the value. 

Then both objects have the same number which will function as a connection. You can use EntityFieldQuery to find the related objects whenever you have the campaign nid.

This is pretty crude though, hopefully there is a smarter way.

Here is some sample code (untested, probably will need a little debugging, paths are from memory).

function my_create_a_node_form() {
  //form stuff goes here.

}

function my_create_a_node_form_submit() {
  //menu_get_object retrieves the current objects that loaded. We're storing it
  // we're storing it in a variable.

  $currentcampaign = menu_get_object();

  //we collect the nid from the campaign.
  $currentnid = $currentcampaign-&gt;nid['value'];
  global $user;

  $values = array(
    'type' =&gt; 'order',
    'uid' =&gt; $user-&gt;uid,
    'status' =&gt; 1,
    'comment' =&gt; 1,
    'promote' =&gt; 0,
  );
  $entity = entity_create('node', $values);

  $ewrapper = entity_metadata_wrapper('node', $entity);
  $ewrapper-&gt;title-&gt;set('YOUR TITLE');

  //here we store the nid in our pre set-up order field.
  $ewrapper-&gt;field_my_order_ref-&gt;set('$currentnid');

  //the rest of this is just standard create a node stuff.
  $my_body_content = 'A bunch of text about things that interest me';
  $ewrapper-&gt;body-&gt;set(array('value' =&gt; $my_body_content));
  $ewrapper-&gt;body-&gt;summary-&gt;set('Things that interest me');

  $my_date = new DateTime('January 1, 2013');
  $entity-&gt;field_my_date[LANGUAGE_NONE][0] = array(
     'value' =&gt; date_format($my_date, 'Y-m-d'),
     'timezone' =&gt; 'UTC',
     'timezone_db' =&gt; 'UTC',
   );

  $ewrapper-&gt;save(true);
  entity_save('node', $entity); 

}