2

I'm building a form for inputting people's travel plans, and as part of that, I need to capture who will be in what country on what days (see diagram for relevant section of the form)

Itinerary Collection Fields

Next to the Add Another Item button to allow me to add another top-level itinerary, I would like to add a 'duplicate last itinerary button' that would add another item to the itinerary list, but copy the contents of the last itinerary item into it. Ideally, the button would only copy the data in the section surrounded by a dotted line on the diagram (the itinerary leg content) and leave the traveler(s) field blank.

If there is a ready module to do this, that would be fantastic, but I'd be perfectly happy for a code-based solution or failing that, pointers to good info.

I've found How to pre-populate multiple "field collection" fields in node form which in spite of being closed seems like it may contain good information on populating the entries. I think I need to know how to do basically 3 things and what hooks make sense to accomplish them:

  1. add a button alongside the other multi-entry field buttons
  2. read the contents of the last itinerary leg field collection field in the list of itineraries.
  3. add a new item, and populate the itinerary leg field collection fields with the values from step 2.
  • add a 'duplicate last itinerary button' that would add another item to the itinerary list, but copy the contents of the last itinerary item into it so in other words, copy the values of the itinerary that is above it? – No Sssweat Jan 30 '17 at 8:24
  • If by it, you mean the button, then yes the last itinerary in the list would be the one above the button, except that I would prefer to only copy the itinerary leg field collection in that last itinerary and not copy the user entity reference field that is also part of the itinerary collection. – UltraBob Jan 30 '17 at 8:27
  • 1 use hook_form_alter, for 2 & 3 with jQuery. using click function var country = $(#some-div-class:last-child).val(); (hopefully this will grab the last one before the ajax is complete) then ajaxcomplete() $(.some-div-class:last-child).val(country); in reality you will need to do something like $(.some-div-that-wraps-each-collection:last-child).children().next().val(); to get to the field. – No Sssweat Jan 30 '17 at 9:07
  • I need to copy a start date and end date, a country, and a notes field for each leg of the itinerary, then create a new itinerary item that contains the values for all of those legs. Is jQuery still the right way to get all those values? – UltraBob Jan 30 '17 at 12:42
  • 1
    see option 3 – No Sssweat Feb 1 '17 at 7:36
0

I think you should be using the Replicate API for this purpose. The module provides an API to duplicate entity along with many hooks. It's targeted towards developers however there are several modules build on top of it.

With respect to your use case, there is an implementation for supporting field collections. I think it will be the closest solution for you what you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.