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.
Creating field collection programatically
Creating field collection programatically
Comments
Comment #1
tcalin CreditAttribution: tcalin commentedHello!
I am trying to create a field_collection from the code (a line on an invoice to be more exactly)
The sequence looks like this:
but I get the following exception from
save
function:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '122' for key 'PRIMARY'
Am I missing something?
Thanks in advance!
Comment #2
ogi CreditAttribution: ogi commentedsubscribe
Comment #3
ogi CreditAttribution: ogi commentedThis worked for me:
Comment #4
tcalin CreditAttribution: tcalin commentedThanks!
I finally got the time to test it and it worked for me too.
Anyway, my code should also be OK.
Comment #6
tcalin CreditAttribution: tcalin commentedThis solution does not work anymore.
Comment #7
rbruhn CreditAttribution: rbruhn commentedThought I would post my solution for programmatically creating a field collection, along with an attached field, during a module install. The example is specific to my use, but easily modified to add the field types you need. Of course, if there is an easier way please feel free to add your thoughts. This works with the current 7.x-1.x-dev branch.
And as stated above, this worked when needing to add data programmatically.
Comment #8
phreestilr CreditAttribution: phreestilr commentedThanks! Very helpful
Comment #9
jibize CreditAttribution: jibize commentedThank you!!
Comment #10
JMOmandown CreditAttribution: JMOmandown commented@rbruhn: Sorry to reactivate an old thread. I was curious if you knew of any issues with field collection creation in the traditional field array method as you have described when in a batch operation. The following throws a HTML general error and has had me stumped for the past few days.
Comment #11
rbruhn CreditAttribution: rbruhn commented@JMOmandown - Well, I took your code above and changed a few things so I could run it in a test.php file using drush on my system. The code you posted is simply attempting to create Field Collections, and not attaching fields to those collections. So I'm assuming that is what you wanted. In the code below:
1) I changed the entity_type and bundle because I do not have commerce installed.
2) I refactored the code since you are only creating one Field Collection at a time when calling the function. So there is really no need to use a foreach loop if you don't have to.
3) As well, since I'm merely testing the function, I removed the other unneeded variables being passed to the function so it would not throw an error while running the test script. I know you are doing this in a batch so yours is different, but perhaps the below will help you find out what is wrong.
Anyway, the below creates the Field Collections for me. I see them added in my UI, as well as being present on the Article node form.
Hope this helps.
Comment #12
JMOmandown CreditAttribution: JMOmandown commented@rbruhn: Thanks for the quick response. As I feared it is still not functional which is so weird to me. If I comment out the function above (one of many operations in the batch) then the batch runs smoothly. With it enabled I get and error with no repsonse text:
So weird, but I thank you for your quick response and attempt.
Comment #13
rbruhn CreditAttribution: rbruhn commented@JMOmandown: Yes, I hate those messages that don't really give you anything to work with. I think they are trying to fix some of those in D8. When I run into your problem, I use to place some code to spit out to a file or something so I can at least see at what point it's failing. For example, writing to file when it creates the field or instance and the name of the field. From there, following to each function called so I can get the exact point. I could usually figure out what is going wrong.
Might be possible to use drush to run your batch (http://drupal.org/node/873132) with debug enabled. Since I've started using drush, errors have been a lot easier to ascertain.
Comment #14
thtas CreditAttribution: thtas commentedI'm having success with this method
Comment #15
dgtlmoon CreditAttribution: dgtlmoon commentedTo set multiple field collection values, just put the field_collection entity stuff in a loop
hope it helps someone!
Comment #16
codesmithI just had an issue where trying to add a new field collections to the 'user' entity would delete any existing items. The solution was to directly load the user entity and not depend on the global $user as that one doesn't have the field collection information.
Comment #17
Thomas83 CreditAttribution: Thomas83 commentedfirst of all, English is not my native language so sorry if this reads bad.
I'm trying to program a field collection with field collection item fields like the code as given by rbruhn here: https://drupal.org/comment/4653514#comment-4653514
I want to create it on an easier leavel for testing purposes. My goal is to create a taxonomy reference dropdown and a textfield.
I manage to reproduce the field_collection type. Hower, I can't reproduce any field collections item fields. Let's say I want a textfield in my field collection.
creating an ordinary textfield field collection item field, this would make sense to me (but it doesn't work). Can someone tell me what I do wrong? Instead of appearing inside my field collection it appears as a field inside my node which contains my field collection field
Comment #18
pratip.ghosh CreditAttribution: pratip.ghosh commentedFor anyone wondering how to save a fieldcollection data which is within another field collection data, it may come to some help...
Comment #19
petu CreditAttribution: petu commentedHere is the code for multiple values inside the field collection entity:
Comment #20
jwilson3Thanks to all previous commenters, this thread helped me get what I needed done. However I ran into error message #1822844: Notice: Undefined index: revision_id in field_collection_field_get_entity() (line 1586 of field_collection/field_collection.modu and found that if I used:
Instead of:
The errors went away.
Comment #21
ethanLee CreditAttribution: ethanLee commentedGreat solution for me. But I got a new problem. This method only save one record for $field_collection_item['und'][0]['value']. If I want to save $field_collection_item['und'][1]['value']. What should I do? Please help! Thank you.
Comment #22
DieterAtWork CreditAttribution: DieterAtWork commentedFor a good tutorial on how to actually do this, with explanations, look at this: https://www.drupal.org/node/1842304#comment-9295475
Comment #23
jienckebd CreditAttribution: jienckebd commentedIf anyone else needs something similar, the below script will make copies of any field collections on your site. In this case, it creates 2 copies for foreign languages (Spanish and Portuguese). It also adds field instances that were on the original field collection.
Comment #24
artatum CreditAttribution: artatum commentedHello
There is useful things here, but I dont find how actually creating a fc in a module : where do we declare the fc, I mean what is the right hook_???_info for fc ? Is there any beginner sample module code somewhere ?
Comment #25
kenorb CreditAttribution: kenorb commentedSee: Entity metadata wrappers
Comment #26
logicp CreditAttribution: logicp commentedartatum: You would declare appropriate functions in the module and then have them called for specific circumstances. These circumstances could include the submission of a form, a call to Services API, a cron job retrieving data from a database, etc. The answer all depends on what you are trying to create and under what conditions.
Comment #27
Napche CreditAttribution: Napche as a volunteer and at Sopra Steria commentedA little sidenote :
I was struggling with this to prepopulate an entityForm using the prepareEntity function.
In the end I found out that setHostEntity actually saves the host entity, so it might be wise to save that function till the end.