Currently, using the services module, if I have a field collection implemented on a node, and I call a service to get all the data for that node, I get an xml portion like:
<field_collection_field_name_here>
<und is_array="true">
<item>
<value>1</value>
</item>
<item>
<value>2</value>
</item>
</und>
</field_collection_field_name_here>
where presumably those item -> values reference back to the various sub-values for the field collection.
when a page is displayed, these values are resolved.
I'm wondering what would be involved in resolving those values, say, when node_load is called on the host entity (that's the entity that contains the field collection (which is itself an entity, right), if that would be appropriate? Because that seems to be where services needs it.
I know this is a bit vague, but that's where I'm at.
Any ideas?
Comments
Comment #1
donundeen commentedoh dang, the xml was corrupted:
Comment #2
donundeen commentedOr alternatively, if someone could give me some insight into HOW the data in the field collections is expanded within the hostentity,
then perhaps the modifications could be made on the services side, which may be more appropriate.
Comment #3
fagoYou can try using the restws module, what should work as field-collection has working entity property integration (see rules-test cases).
Anyway, I think the service should function as the underlying API - treat field-collections as separate entities.
Comment #4
Wassim Ghannoum commentedI tried RESTful web services Module but i can't get the Json file
if i go to http://localhost/testingservices/field-collection/field-orders/1.json i will get the first value of the field collection in an html view and not in JSON format
other entities like user and node are working so i can get a JSON file from http://localhost/testingservices/node/1.json
Appreciate your help
Comment #5
Wassim Ghannoum commentedi Think it is working now:
using this format, http://localhost/testingservices/field_collection_item/3.json
Comment #6
paul kim consulting commentedA few issues with just pointing people to restws:
- restws is incompatible with UUID, which is necessary when exporting to different drupal environments.
- The deploy module is heavily dependent on services. Deploying entities with field collections do not work.
- If a site is dependent on services, there is no reason for it to migrate to restws, and break it's api.
Comment #7
paul kim consulting commentedCross referenced here: http://drupal.org/node/1621396
Comment #9
star-szrLet's keep this open, still a valid feature request I think.
Comment #10
hypertext200This may help. This can be easily achieve like 1621396#3 example and I'm looking forward to create a patch for field_collection module.
Comment #11
hypertext200Here is the patch which implements Services module hook. The XML or JSON should contain wrapper element
<fieldcollection>as below.field_collection_item_field['und'][0]['fieldcollection'] = array(ITEMS);Comment #12
darrenmothersele commentedIn addition to comment #6 I'll point people toward this issue http://drupal.org/node/1817956
Deploying entities relies on services. Deploying entities with Field collections is not currently possible.
Entity dependency module defines a dependency between entities with field collections and the actual field collection item entities.
Deploying these entities attempts to deploy the dependencies first. This fails because there is no service resource to receive them. I defined a simple service to do this, then uuid_services takes over and does it's stuff - but it fails because saving a field collection entity requires it's host entity. Obviously the host entity is not available yet, because the field collection item is deployed first due to the entity_dependency.
Comment #13
gumdal commentedHi,
I have applied your patch. After applying the patch, I have triggered a test webservice request and I dont see any change in my webservice response. I again get the same response like this:
This is the part of XML response which am still getting the same after applying your patch:
What I am trying to get is the contents of fields collection right inside the
tag's contents instead of the value 2 & 3. What else am I supposed to do?
Thanks,
Raj Pawan G
Comment #14
gumdal commentedAny updates about the issue? I am badly in need of a fix for this, am not a PHP programmer and so need help. Thank you.
Comment #15
gumdal commentedThis patch (#11) unfortunately did not work for me as expected. Please check my below response on 25th Nov. I did not know then to change the status.
Thank you.
Comment #16
stupiddingo commentedUsing the latest releases of services and field_collection when calling /myrestserver/node/1 $arg holds the nid, not an array so when passed to _service_fieldcollection_recursive_search it fails.
What array should be passed to _service_fieldcollection_recursive_search?
Comment #17
gumdal commentedUsing the concept from #11 and the Entity loading concept, I could pull the field collection information into the node XML (or JSON) in the webservices, I am using the latest beta 5 7.x-1.0 source. The modification done is not in a conventional way which would serve general purpose, I had to hardcode the field names in my code which I will explain later why I did it so.
First thing, had to implement the hook
field_collection_services_request_postprocess_alter()instead offield_collection_services_request_preprocess_alter()as used by heshan.lk probably coz the way function calls work in services 3.0 is changed. I could observe that preprocess never get triggered here. Next, as mentioned by #16 there is an extra parameter now$optionswhich actually holds the value instead of$args. Also, now we get the object collection in$optionsvariable as stdClass, we need to convert it to array in order to enumerate through it.Next is a function which recursively checks in each of the element (and its children) if there is an entity (which is what field_collection is represented with). A recursive method is written which loads the entity and pulls out the entity information to prepare the field collection information into main node. I had to hardcode my field names here because once the entity is loaded, I could see many entity specific fields along with them is the user defined fields too. But none of those entity fields had information about what is the user defined field name with which entity is associated with. I would like to know if there is a way to identify the user defined field name so that I could provide a generalized solution for it and perhaps we can provide a patch to this.
Next is a small helper method to convert object collection into array:
Please observe that theI was wrong earlier, my test case about this failed earlier and now I could debug and confirm that there is no dependency as such between$theNodeis not used but I think there is a dependency on usage ofentity_load()which needs the node to be loaded(?). Not sure about how it works but if I take out the call fornode_load()the entity fetch seems to fail.node_load()andentity_load(). I have updated the code too accordingly.What I would like to do is to make it generalized so that we can give a good patch for it. I can confirm it works in my scenario with the field names specific to my case. However, I am concerned about the points mentioned in #6 and #12 which I could not pretty much understand. I would like experts to let me know if this approach is fine to load the entities and fetch field collection information.
Last but not the least, had to setup PHP debugging for Mac OS X. It was a first time experience for me and thought it would be best to archive the procedure: http://gumdal.blogspot.in/2013/09/setup-for-debugging-php-code-on.html
Comment #17.0
gumdal commentedWrapping XML with code tags
Comment #18
shenzhuxi commentedSInce has already done a good job, I added field_collections relationships to all the entity resources.