Closed (fixed)
Project:
Field collection
Version:
7.x-1.0-beta1
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
5 Jun 2011 at 09:21 UTC
Updated:
27 Jun 2013 at 19:09 UTC
Hi
I want to add a "field collection" dynamically. But I'm not familiar with Field API or Entity API. New Entity API in Drupal is very poorly documented. For example "entity_create" is not documented at all.
I would appreciate if someone could show me the right path to add a field collection by script.
Comments
Comment #1
Mamouri commentedHere is my code, until now:
"Field Collection" module use function "entity_form_submit_build_entity" which I cannot use because there is no form in my case.
I would appreciate if you can tell me how can I add fields?
Comment #2
Mamouri commentedHere is what I achieved and it seems it works:
I would appreciate if you can confirm this is totally right or if it could improve, improve it.
Comment #3
tcalin commentedI use the same sequence and it worked for me until I upgraded recently to Entity API 7.x-1.0-beta9 when everything broke.
Now wither saves only some of the field-collection fields or it simply blocks until exceeds the maximum execution time.
I am still invetigating the issue.
Comment #4
rbruhn commentedNot sure if this will help but I updated #1106182: Creating field collection programatically with an example of what I'm doing programmatically. It's for the 7.x-1.x-dev branch though.
Edit: Using Entity API 7.x-1.0-beta9 too
Comment #5
tcalin commentedHave you tried this in the configuration Drupal 7.2 Entity API beta9?
I get strange results like field collection saved partially or not at all.
Comment #6
tcalin commentedIn some situations I get this error from apache. There is a recursion that seems to go forever
In other situations the field-collection is saved but with some fields missing.
Comment #7
rbruhn commentedI'm using Drupal 7.2 and Entity API 7.x-1.0-beta9
Had to edit my post on the other issue. I forgot to replace $key with $field['instance']['field_name'] when checking for the field instance.
But other than that, it works for me.
Comment #8
tcalin commentedI use your sequence in two situations:
- in the first one the field collection is saved except two fields. It is strange that the form works ok.
- in the second one the field collection can not be saved no mather I try from code or using the form. It fails with the message from #6
Both pieces of code used to work before Entity API beta 9.
This is certainly a bug but It is not very clear for me where to report it.
Comment #9
rbruhn commentedPerhaps uninstall Entity API and reinstall it? Maybe something got corrupted or is cached.
Edit: I'm about to run a migration script to move 1000 records. Will let you know how it goes.
Comment #10
tcalin commentedI have tried this but without result.
We have this announce on the project's page.
Important note:
Entity API version 1.0-beta9 comes with some API changes for modules using it to implement exportable entities, such as Rules, Profile2, Search API, Organic groups, WSClient and Message.
When updating, you have to update all of those modules to their new versions which are compatible with beta9 at once. When doing so, update the code of all modules and then run update.php!
Seems like a good point to start the investigation.
Comment #11
rbruhn commentedRan my migration script and everything went in correctly. The code below is the exact code I used during migration. Understand.... my custom node is called "bir_collection," and that my "field collection" is also called bir_collection. I then attached bir_collection_node to the field collection.
$result is a DB object from the legacy database.
$nids are the new node ids for the old database objects that have been migrated. So, my collections have multiple nodes in them. If you wonder why I used field collection instead of just a normal field for the node reference, it's because I'm planning on adding other fields later.
Anyway... it worked. Not sure why you are having problems.
Comment #12
tcalin commentedI have tried using your sequence. I also used the one from below which is a little bit different.
No way I can get crmtsk_tsk_unit_price and crmtsk_tsk_total_price saved.
Comment #13
lucor commented#12 works for me. I'm using Entity API 7.x-1.0-beta9 and Field Collection 7.x-1.x-dev branch.
Comment #14
tcalin commentedFinally it worked for me too.
But I still have an entity which does not work no matter I try from the script or using the form.
It seems that someone here http://drupal.org/node/1203018 is having the same problem as me.
Comment #15
phreestilr commentedThanks, rbruhn!
Comment #16
tim.plunkettSo, this is fixed?
Comment #18
cornel.eftimie commentedthis is the best solution for working with field collection, it works great!
http://rajanmayekar.com/blog/programmatically-creating-deleting-modifying-field-collection-item-node
Comment #19
hvasconcelos commentedThanks for sharing, that helped!