Closed (fixed)
Project:
Entity reference
Version:
7.x-1.0-beta3
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
30 Dec 2011 at 01:03 UTC
Updated:
1 Jun 2016 at 04:12 UTC
Jump to comment: Most recent
Comments
Comment #1
amir simantov commentedYou helped me with code mate!
Comment #2
haydeniv commentedThe 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:
If referencing a node entity then it would be:
If referencing a taxonomy entity then it would be:
Comment #3
adam_b commentedI'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.
Comment #4
Siripong commentedThank you haydeniv for your information and confirmation. I'm look for these.
^____^
Comment #5
Siripong commentedadam_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.
Comment #6
haydeniv commented@adam_b
The tutorial Siripong referenced is great. The big thing to make sure you have in there is:
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
Comment #7
adam_b commentedThanks Siripong & haydeniv - those look very helpful.
Comment #8
haydeniv commentedThe doc page is up although pretty basic and we have some good info here. I think we can call this one fixed.
Comment #10
killtheliterate commentedHow would I get the target ID, in the method that the autocomplete widget gets it?
Comment #11
Siripong commentedYou can look up nid or tid using a view with exposed filters
like http://drupal.org/node/1370230
or
Comment #12
killtheliterate commentedThanks @Siripong. I ended up doing something very similar to what you had suggested.
Comment #13
aquariumtap commentedIs 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?
Comment #14
haydeniv commented@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.
Comment #15
Yorgg commentedIn this case, how should I do to insert a term into a node programmatically?
Comment #16
haydeniv commented@staminna,
Suppose you have a field called field_term_reference and you have a $node object loaded, all you would need to do is:
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
Comment #17
ryan.merritt commentedFor 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:
Comment #18
Yorgg commentedShouldn'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
Comment #19
sramijan commentedDoes anyone knows how does it works for the References module?
Comment #20
mareq133 commentedValue 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.
Comment #21
jai.kandari commentedworked for me as well. :) thanks
Comment #22
radioflyer commentedIn #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:
In order to set the target_type for a multi-value entity reference field, you should add new references as follows: