verbb / formie

The most user-friendly forms plugin for Craft CMS.
Other
95 stars 73 forks source link

Can individual repeater row fields be mapped in EVENT_MODIFY_FIELD_MAPPING_VALUES? #1264

Closed jamesmacwhite closed 1 year ago

jamesmacwhite commented 1 year ago

Question

I have a form which has several dropdowns of options which can be provided up to 4 times, as on the receiving CRM side there are Choice 1,2,3,4 fields. However, depending on certain criteria these options can be two different sets depending on the type of enquiry but having both options available under one dropdown would be a bit confusing. To solve this I was going to use two repeaters, one for each of the options.

It works well from UX/form management but it means that the choices cannot be mapped on the form settings directly anymore because they are not single dropdown fields. However is it possible to specify a specific repeater row in the field mapping to assign it upon submission? So to essentially target the specific repeater row field and assign it into the mapping against the choice 1,2,3,4 fields.

In this case the repeater will only ever have a maximum of 4 blocks and the repeater row content will always be a sequence of 1,2,3,4, therefore it should be possible to always determine which repeater row should be where, mapped against the relative choice field in the sequence. An additional bit of logic will need to be determine the right repeater based on other field values in the submission, but that can be done without an issue. It is more to do with being able to specify a specific repeater row field based on index.

Thanks!

Additional context

No response

jamesmacwhite commented 1 year ago

So I ended up doing something like this in the event:

$typeOfEnquiry = $submission->youAreEnquiringAboutTheFollowing->label;
$subjectAreas = $submission->subjectAreas->all();
$apprenticeshipAreas = $submission->apprenticeshipAreas->all();

if ($typeOfEnquiry === 'Apprenticeships') {
    $subjectArea1 = $apprenticeshipAreas[0]->apprenticeshipArea->value ?? null;
    $subjectArea1Other = $apprenticeshipAreas[0]->otherApprenticeshipArea ?? null;
    $subjectArea2 = $apprenticeshipAreas[1]->apprenticeshipArea->value ?? null;
    $subjectArea2Other = $apprenticeshipAreas[1]->otherApprenticeshipArea ?? null;
    $subjectArea3 = $apprenticeshipAreas[2]->apprenticeshipArea->value ?? null;
    $subjectArea3Other = $apprenticeshipAreas[2]->otherApprenticeshipArea ?? null;
    $subjectArea4 = $apprenticeshipAreas[3]->otherApprenticeshipArea->value ?? null;
    $subjectArea4Other = $apprenticeshipAreas[3]->otherApprenticeshipArea ?? null;
}
else {
    $subjectArea1 = $subjectAreas[0]->subjectArea->value ?? null;
    $subjectArea1Other = $subjectAreas[0]->otherSubjectArea ?? null;
    $subjectArea2 = $subjectAreas[1]->subjectArea->value ?? null;
    $subjectArea2Other = $subjectAreas[1]->otherSubjectArea ?? null;
    $subjectArea3 = $subjectAreas[2]->subjectArea->value ?? null;
    $subjectArea3Other = $subjectAreas[2]->otherSubjectArea ?? null;
    $subjectArea4 = $subjectAreas[3]->subjectArea->value ?? null;
    $subjectArea4Other = $subjectAreas[3]->otherSubjectArea ?? null;
}

// Map our values when we get to the lead mapping, check for one of the subject area fields
if (array_key_exists('ccl1000_subjectareaid@odata.bind', $event->fieldMapping)) {
    $event->fieldValues['ccl1000_subjectareaid@odata.bind'] = $subjectArea1;
    $event->fieldValues['ccl3016_subjectareaother'] = $subjectArea1Other;
    $event->fieldValues['ccl1000_subjectarea2@odata.bind'] = $subjectArea2;
    $event->fieldValues['ccl3016_subjectareaother2'] = $subjectArea2Other;
    $event->fieldValues['ccl3016_subjectarea3id@odata.bind'] = $subjectArea3;
    $event->fieldValues['ccl3016_subjectareaother3'] = $subjectArea3Other;
    $event->fieldValues['ccl3016_subjectarea4id@odata.bind'] = $subjectArea4;
    $event->fieldValues['ccl3016_subjectareaother4'] = $subjectArea4Other;
}

It seems to work fine. Not sure if it's the best way, the main issue was being able to insert the field values at the correct point for each payload, as otherwise it would go onto the wrong entity.

engram-design commented 1 year ago

Hmmm, so that's tricky. There's no way to specify a row for the repeater field to map, because it's not always guaranteed to be there. Using the event as you've done is perfectly fine for this, and encouraged!

jamesmacwhite commented 1 year ago

@engram-design Awesome, glad I'm on the right track! It does seem to work OK. In this case because there is a limit and sequential it can be mapped accordingly to that which won't ever change.