I have a quite complex node type with entity reference fields and nested field collections. When I display the fields as a JSON or XML view, the referenced content get displayed as stripped HTML, i.e. more or less as they would be displayed in an unformatted view. What I expeced was that the JSON format would trickle down the reference chain. I suspect the problem could be fixed some kind of JSON/XML view mode. Has anyone encountered this?
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | views_datasource-field_collection_array-1348670-13.patch | 2.94 KB | dejan0 |
| #9 | views_datasource-field_collection_array-1348670-9.patch | 1.17 KB | roflcopterDorrie |
Comments
Comment #1
camdarley commentedYes, same problem here. All my collection fields containing reference fields are broken.
Comment #2
Chipie commentedAny solutions?
Comment #3
escore commentedI ended up writing my own function that uses
entity_metadata_wrapperto get an object that allows me to traverse down the node's field collections, get the values I want and build a new array that I then pass todrupal_json_encodeordrupal_json_output. That way I can also exclude all the fields in the original node that I don't want in the output. It's no general solution, though, since my code is written specifically for the content type I want to parse. I haven't tried to integrate it with views, but by writing a menu callback function in my custom module, that in turn calls my json formatter, I got the functionality I wanted.Comment #4
camdarley commentedCan you share your code? I was following this way too to find a solution, but with no result.
Comment #5
alexander allen commentedThis is valid, I've been through this.
Please note that there is an ongoing effort at #1699368: #RVDA: Rewrite Views Datasource's Architecture and #1623606: Views Datasource 2.x branch to fix issues like this one.
Feel free to join the #VDS-A tag testing party - it will contain a fix for this.
Comment #6
alexander allen commentedComment #7
doub1ejack commentedUntil we get an improved version of this module (kudos to A.Allen!), the best alternative I have found is to include the field_collection (or similar) fields individually. By creating a relationship in your view to whatever is giving you trouble (a field_collection in my case), you will be able to access the individual fields and configure the field settings to remove the presentation markup.
There are a few reasons this is not an ideal solution:
But hey, I didn't have to write a module or any SQL : /
Comment #8
kenorb commentedI've field collection with two fields inside: field_title (single valued) and field_feed_collection (multiple valued) which I wanted to be hierarchical.
So I've solved it by implementing
hook_preprocess_views_view_field:Then in template file I've printed the variable:
Then the output would be like:
Comment #9
roflcopterDorrie commentedI have created a patch for JSON to output field collections in an array rather than the rendered collection.
It renders each field according to the view mode set in the views field.
It currently uses the machine name as the label.
I hope its of some use :)
You must apply patch https://www.drupal.org/node/1881670#comment-10473816 first.
Comment #10
studio-days commentedHey @rolfcopterDorrie - crazy that you just posted this as it's just what I'm looking for. However it's not working for me. I've already installed the other patch that you mention (that one works), then installed yours, but still getting a Field Collection outputting all as a string.
Can you provide more details as to how you got this working? What Formatter option do you have selected in the views field?
I also got a fuzz 1 when installing the patch... Could that be the issue?
Comment #11
studio-days commentedI worked it out!
It doesn't work when the Field Collection field on the entity is limited to 1 value.
It does work for unlimited values (which is what I need, yay!).
So I'm all good, thanks! But you may want to fix it to work with just 1 value as well.
Comment #12
studio-days commentedSo now the BIG question is: can the patch in #9 be repurposed to work with Term Reference fields? IE a Term, with its own set of fields, is pretty similar a Field Collection. If this Term with its fields could come through as a nested array, then we can really use Drupal muscle to create proper JSON...
Comment #13
dejan0 commentedSo, patch #9 worked correctly only for the first level of collection fields. But collection fields can be nested, so field_collection fields themselves can have another field_collection field as a child, and so on.
Please find attached an improved version of the previous patch, so it recursively lists all nested collection fields/values.
Again, you have to apply the https://www.drupal.org/node/1881670#comment-10473816 patch before.