I initial a howto page at http://drupal.org/node/1388922

  $node = new stdClass();
  $node->title = "YOUR TITLE";
  $node->type = "YOUR_NODE_TYPE";
  ....

  $node->field_customer_nid[$node->language][]['target_id'] = $form_state['values']['entity id'];

  ...
  node_submit($node);
  node_save($node);

It works fine. But I'm not sure is it the correct way to fill the entity reference field?
Should I set another thing else to make the module works properly?

Comments

Amir Simantov’s picture

You helped me with code mate!

haydeniv’s picture

The above should work fine if you are just targeting an entity but when I needed to target a user, node or taxonomy term I needed to specify target_type as well:

$node->field_customer_nid[$node->language][]['target_id'] = $form_state['values']['entity id'];
$node->field_customer_nid[$node->language][]['target_type'] = "user";

If referencing a node entity then it would be:

$node->field_customer_nid[$node->language][]['target_id'] = $form_state['values']['entity id'];
$node->field_customer_nid[$node->language][]['target_type'] = "node";

If referencing a taxonomy entity then it would be:

$node->field_customer_nid[$node->language][]['target_id'] = $form_state['values']['entity id'];
$node->field_customer_nid[$node->language][]['target_type'] = "taxonomy_term";
adam_b’s picture

I'm actually just trying to work out how to do this. Could I ask someone to give the abbreviated For Dummies version of instructions?

Ideally I'd like something like noderefcreate where you type into an autoselect field and if nothing matches it creates a stub automatically.

Siripong’s picture

Thank you haydeniv for your information and confirmation. I'm look for these.
^____^

Siripong’s picture

adam_b

Have you try create basic node like http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-dr...
After complete that lesson, you could add a entity reference field like what I show you above.

haydeniv’s picture

@adam_b

The tutorial Siripong referenced is great. The big thing to make sure you have in there is:

node_object_prepare($node); // Set some default values

That gets the node ready with all of the default values that you don't need to set.

Here is another tutorial with a bit more detail that I used as well.

http://timonweb.com/how-programmatically-create-nodes-comments-and-taxonomies-drupal-7

adam_b’s picture

Thanks Siripong & haydeniv - those look very helpful.

haydeniv’s picture

Status: Active » Fixed

The doc page is up although pretty basic and we have some good info here. I think we can call this one fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

killtheliterate’s picture

How would I get the target ID, in the method that the autocomplete widget gets it?

Siripong’s picture

You can look up nid or tid using a view with exposed filters
like http://drupal.org/node/1370230

or

  $query = new EntityFieldQuery;
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'YOUR-CONTENT-TYPE')
    ->propertyCondition('title', $form_state['values']['title'], 'LIKE')
    ->execute();

  foreach($result['node'] as $nid => $node){
    $node = node_load($nid);
    ....
  }

killtheliterate’s picture

Thanks @Siripong. I ended up doing something very similar to what you had suggested.

aquariumtap’s picture

Status: Closed (fixed) » Active

Is there any more information / documentation on 'target_id' and 'target_type'? Where did they come from? Is there an API function available, or do we have to work with the raw data structure this way?

haydeniv’s picture

Status: Active » Closed (fixed)

@aquariumtap
Currently you just need to deal with the raw data structure. There is no API to deal with them directly as of yet. Target_id and target_type are created when you make an entity reference field. Create a node and use Devel module to examine the entity reference field and you will see how they are used.

j_nunes’s picture

In this case, how should I do to insert a term into a node programmatically?

haydeniv’s picture

@staminna,
Suppose you have a field called field_term_reference and you have a $node object loaded, all you would need to do is:

$node->field_term_reference['und'][0]['target_id'] = [Enter_your_TID_here];

und is the language you want to use and in this case refers to the site default language.
0 is the delta for the field so if you were associating multiple terms in the same field you would increment the delta for each tid.

See item 1.5 on this page: http://timonweb.com/how-programmatically-create-nodes-comments-and-taxonomies-drupal-7

ryan.merritt’s picture

For anyone seeing this block of code, trying it, and then getting PDO exceptions (the all white error page, as i know it) remove the 'target_type' line and give it a try.

I'm currently using it for taxonomy terms and it works.

REMOVE:

$node->field_customer_nid[$node->language][]['target_type'] = "taxonomy_term";
j_nunes’s picture

Shouldn't that line be the one who's inserting the term into the reference field?
Along with other notice, I get PDOException: SQLSTATE[23000]: Integrity constraint violation.
If you remove this line you mention, which one will insert the tag into the node's field?

Thanks

sramijan’s picture

Does anyone knows how does it works for the References module?

mareq133’s picture

Value of the $node->myentity_reference_field['und'][0]['target_id'] needs to be integer.

Are you using CER (Corresponding Entity References) module? check the "cer" table in database and remove all entries for you entry reference field. This was causing the problem in my case.

jai.kandari’s picture

Issue summary: View changes

worked for me as well. :) thanks

radioflyer’s picture

In #2, the target_id and target_type end up with different array indexes (deltas when saved).

In order to set the target_type for a single-value entity reference field, you should set the delta manually as follows:

$node->field_customer_nid[$node->language][0]['target_id'] = $form_state['values']['entity id'];
$node->field_customer_nid[$node->language][0]['target_type'] = 'node';

In order to set the target_type for a multi-value entity reference field, you should add new references as follows:

$node->field_customer_nid[$node->language][] = array(
  'target_id' => $form_state['values']['entity id'],
  'target_type' => 'node',
);