Right now, if you have a field collection on a node and you export it using D8's REST functionality (either by adding a ?_format=json parameter, or doing a REST export in Views), you get a stub:
"field_fc_project": [{
"value": "1",
"revision_id": "1"
}]
We can implement a custom Normalizer to tell Drupal to return the full field collection item, instead of just value and revision_id. Something that looks a little more like...
"field_fc_project": [{
"item_id": [{
"value": "1"
}],
"host_type": [{
"value": "node"
}],
"uuid": [{
"value": "24371318-6ab6-4833-ac00-f95df38d46d7"
}],
"revision_id": [{
"value": "1"
}],
"field_name": [{
"target_id": "field_fc_project"
}],
"field_some_custom_field": [{
"value": "This is my string field that I added"
}]
}]
Comments
Comment #2
charginghawk commentedHere's my patch to accomplish the above, modeled after: https://api.drupal.org/api/drupal/core!modules!serialization!src!Normali...
This doesn't seem to work with HAL output though. That may need an additional normalizer.
Comment #3
tim.plunkettThanks! This could use some test coverage though.
Comment #4
tim.plunkettComment #5
ashopin commentedI tried to apply the patch on the latest Dev version and got the following error when I try 'drush cr':
Comment #6
marcel.jtf commentedPatch works fine when format = entity. It does not work when format = fields.
Comment #7
charginghawk commentedmarcel.jtf - Field format is a separate issue. See:
#2574077: REST Export display cannot show any raw output for fields using the Field field handler
#2637312: REST views: using DataFieldRow's "raw_output" option causes entity reference fields to output NULL
For my workaround for Field format, see: https://www.drupal.org/node/2637312#comment-10726700
Comment #8
jmuzz commentedI've looked briefly at the possibility of making tests for this. FieldCollectionBasicTestCase will start getting unwieldy if more tests keep getting added to it, so I created #2760431: Move setup and helper functions out of FieldCollectionBasicTestCase as a soft pre-requisite for making these tests.
Comment #10
jmuzz commentedThanks @charginghawk. I'm going to leave this open now as HAL still isn't supported. REST is a big topic. It might be worth making separate issues for the other formats and request types and a meta issue for them.
Comment #12
jmuzz commentedDevelopment is moving to 8.x-3.x.
Comment #13
paranojik commentedThis should take care of HAL support. Co-authored with @das-peter.
Comment #15
das-peter commentedHmm, not quite sure how we resolve the dependency to
rest.link_manager. I don't think we want to introduce a dependency as it's not required to run the module itself but only if we want to run with HAL.I think we can add the new HAL normalizer conditionally - just if HAL is enabled. I assume we can do this using code similar to the one in the Default Content module which provides a ServiceProvider:
Comment #16
paranojik commented...there. This should fix the HAL dependency as proposed in #15. @das-peter, thanks for all the assistance!
Comment #17
das-peter commentedWoohoo, sweet thanks for the patch.
I like the patch and couldn't find things to nag about - but wont set to RTBC as I'm to closely involved with what was created ;)
Comment #18
alanyong commentedWhat i get is HTML in JSON.
\n\n\n <div class=\"field field--name-field-mall field--type-entity-reference field--label-hidden field--item\">7</div>\n \n <div class=\"field field--name-field-address field--type-string-long field--label-hidden field--item\">20, Level 5</div>\n \n <div class=\"field field--name-field-state field--type-entity-reference field--label-hidden field--item\">Kuala Lumpur</div>\n \nhow to fix this ?
Comment #19
wim leersWithout this, you can't interact with field collections via REST (or JSON API for that matter).
I had https://www.drupal.org/u/susannecoates send me an e-mail asking about this, but I'm a maintainer of the core REST module, and Drupal core obviously doesn't have field collections :)
Also marked #2824012: Rest-Support as a duplicate of this.
Comment #20
wim leersThis looks very scary. It also seems to be polluting the public API (but I don't know
field_collectionwell enough to judge this).This can probably be removed entirely by setting the
$formatproperty.FYI: for tests, you can find examples of how to create functional test coverage for a specific field type/normalizer in:
\Drupal\Tests\datetime\Functional\EntityResource\EntityTest\EntityTestDateonlyTest\Drupal\Tests\datetime\Functional\EntityResource\EntityTest\EntityTestDatetimeTestComment #21
cytherion commentedCommenting on #19
Thanks Wim.
As my issue is related specifically to field collection items I have opened a new issue here: https://www.drupal.org/node/2893924
-Susanne
Comment #22
mlulu commentedI'm facing an issue with posting a field collection item using RESTful API, I used the patch #16 but I'm still getting 403 error access denied
Comment #23
ram4nd commented