Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm trying to add a entityreference field linking two bundles of a same entity using Field API, but the following code does not seem to work (field is created in db and appears in admin UI, but widget and field settings are not set at all):
function booking_install() {
...
$field = array(
'field_name' => 'artist_ref',
'cardinality' => 1,
'type' => 'entityreference'
);
field_create_field($field);
$instance = array(
'field_name' => 'artist_ref',
'label' => 'Artist',
'widget' => array(
'type' => 'options_select',
),
'entity_type' => 'booking',
'bundle' => 'gig',
'target_bundles' => array('artist'),
);
field_create_instance($instance);
}
Here entity type 'booking' contains two bundles 'gig' and "artist', the intent is to add a field 'artist_ref' to gig referencing an artist.
Something must be wrong with my code. Could that basic use case be documented somewhere?
Comments
Comment #1
ParisLiakos CreditAttribution: ParisLiakos commentedthats how i managed to do it,after hours of debugging:/
Comment #2
ParisLiakos CreditAttribution: ParisLiakos commentedPlease add the example, in a README.txt or here in d.o so it will be easier to find and people wont waste hours:)
Comment #3
unistereo CreditAttribution: unistereo commentedCheers rootatwc, I got it working thanks to you!
For the record, this slightly shorter version seems to be working too (at least for me):
Here the 'settings' array is only used once (for field_create_field) and has a slightly different structure.
Can't remember where I stole that code from, nor whether that version is better or worse, but here it is.
Now can only wait on maintainers to approve or suggest a proper method.
Comment #4
ParisLiakos CreditAttribution: ParisLiakos commentedOh weird, i tried this but didn't work :)
thats why i used the settings array on both callbacks.but w/e glad you made it:)
Comment #5
soshial CreditAttribution: soshial commentedIt would be great to have even a simple explanation in readme how to create such fields programmatically!
Comment #6
rogueturnip CreditAttribution: rogueturnip commentedThis worked for me until I tried to view the entity with the reference.
I get the following error when only the label is to be displayed:
Notice: Undefined index: entity in entityreference_field_formatter_view() (line 643 of /Users/tony/Documents/Web Sites/drupal7/sites/all/modules/entityreference/entityreference.module).
If I try to put a link with the label it causes the entity to not show at all.
Any one else have this problem? Am I missing something in the field instance?
Comment #7
giorgio79 CreditAttribution: giorgio79 commentedRelated discussion: #1388924: Programmatically create nodes with Entity reference field I found this thread by searching programmatically adding and removing references....
Comment #8
alexmc CreditAttribution: alexmc commentedI'm not sure if this is the same issue, but can you add and manipulate entity references through the REST Service
Comment #9
tcm5025 CreditAttribution: tcm5025 commentedJust wanted to point out that #1 works and #3 does not. Hope it might help others who come here and use the wrong one first like me.
Comment #10
dragon658 CreditAttribution: dragon658 commentedWorking code (examples above didn't work for me):
Comment #11
Raman Starshykh CreditAttribution: Raman Starshykh commentedHi guys.
There is an easy way to create all the necessary fields
Go to the admin/structure/types/manage/my_type/fields
Create field
Then we call the two functions
The values of these functions are used in our module
Comment #12
blakefrederick CreditAttribution: blakefrederick commentedThank you Raman for #10 and #11. Creating the field in the UI, then running the two functions to get the correct values and structure of the field data saved me hours of debugging. Thank you!
Comment #13
Kris77 CreditAttribution: Kris77 commented@Raman @blakefrederick Where should the two functions be performed ?
Thanks.
Comment #14
Raman Starshykh CreditAttribution: Raman Starshykh commented@Kris77 you can use devel module - execute php code or use in your custom module - custom hook_menu or other
As a result, you get the correct data arrays