0

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!

1 Answer 1

1

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->nid['value'];
  global $user;

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

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

  //here we store the nid in our pre set-up order field.
  $ewrapper->field_my_order_ref->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->body->set(array('value' => $my_body_content));
  $ewrapper->body->summary->set('Things that interest me');

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

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

}

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.