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.
Could someone please provide an example on how to create the fields and add values?
Example: Map (node) has unlimited areas (field collection field). Each area has unlimited layers (field collection field). The area fc. have normal fields and the layer fc. The layer fc. only have normal fields.
Creating and attaching an fc. to a node is fine, but I can't work out how to add another fc. to that fc..
I assume the 2nd fc. should use the 1st fc. id as HostEntity?!
Any ideas, suggestions are welcome. Thanks a lot.
Comments
Comment #1
Greg Varga CreditAttribution: Greg Varga commentedSo it turned out that the solution was very simple:
1) Create the base node (Map) and save it using node_save() -> $map_node
2) Create the first field collection (Area)
The Area fc. contains another fc. called Layers, but for now, we can ignore that. Add the required values, set the HostEntity, which is the $map_node and save the fc.
3) Create the second field collection (Layers)
This is pretty much the same as the previous code, but the HostEntity is pointing to the Area fc. and the HostEntity type is "field_collection_item"
Once everything is saved, you are good to go. Good luck.
Comment #2
Greg Varga CreditAttribution: Greg Varga commented