verbb / formie

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

Unable to map nested fields in custom CRM integration #1849

Closed steverowling closed 2 months ago

steverowling commented 2 months ago

Describe the bug

I have created a custom CRM integration which has field mapping set up in the form settings to map fields from the form to the CRM fields. This includes targeting subfields in the Formie name and address field types like this:

Screenshot 2024-04-24 at 16 50 23

When the integration is applied, I am using the following code to map the submission fields to the CRM fields:

// Fetch the form settings for our field mapping - for each collection of data
$constituentValues = $this->getFieldMappingValues($submission, $this->constituentFieldMapping, 'constituent');
$constituentAddressValues = $this->getFieldMappingValues($submission, $this->constituentAddressFieldMapping, 'constituentAddress');

Example submission values look like this (from the form submission):

Name: First Name: Steve Last Name: Rowling

I would expect that, given the field mapping above, the CRM fields should look like this:

'first' => 'Steve',
'last' => 'Rowling',

Instead, they look like this:

'first' => 'Steve Rowling',
'last' => 'Steve Rowling',

So instead of mapping the nested subfields to the CRM fields, the parent form field value has been applied to each of the CRM values.

This is happening for both native Formie name and address fields when I try to map subfields to CRM fields.

Steps to reproduce

  1. Set up custom integration using instructions from the Formie docs, including defining CRM fields (all are defined as TYPE_STRING).
  2. Create a form and map Formie name and address sub-fields to appropriate CRM fields.
  3. Save a sample submission and run the custom CRM integration. The CRM fields that should be set to the nested subfields of the name and address Formie fields have instead been set to the entire value of the parent Formie name and address fields.

Form settings

Craft CMS version

Craft Pro 5.0.5

Plugin version

3.0.0-beta.8

Multi-site?

No

Additional context

No response

steverowling commented 2 months ago

I've looked into this further and if I comment out the following lines in the verbb\formie\base\SubField.php class, the field mapping works as expected:

    protected function defineValueAsString(mixed $value, ElementInterface $element = null): string
    {
        // Bubble-up to the main field method, as we don't want to use `SingleNestedField` definitions
        return Field::defineValueAsString($value, $element);
    }

    protected function defineValueAsJson(mixed $value, ElementInterface $element = null): mixed
    {
        // Bubble-up to the main field method, as we don't want to use `SingleNestedField` definitions
        return Field::defineValueAsJson($value, $element);
    }

    protected function defineValueForExport(mixed $value, ElementInterface $element = null): mixed
    {
        // Bubble-up to the main field method, as we don't want to use `SingleNestedField` definitions
        return Field::defineValueForExport($value, $element);
    }

    protected function defineValueForSummary(mixed $value, ElementInterface $element = null): string
    {
        // Bubble-up to the main field method, as we don't want to use `SingleNestedField` definitions
        return Field::defineValueForSummary($value, $element);
    }

    protected function defineValueForIntegration(mixed $value, IntegrationField $integrationField, IntegrationInterface $integration, ElementInterface $element = null, string $fieldKey = ''): mixed
    {
        // Bubble-up to the main field method, as we don't want to use `SingleNestedField` definitions
        return Field::defineValueForIntegration($value, $integrationField, $integration, $element, $fieldKey);
    }

Not sure what the other consequences of doing this might be, but it does make the field mapping to subfields of Formie's address and name fields work correctly.

engram-design commented 2 months ago

Fixed for the next release. To get this early, run composer require verbb/formie:"dev-craft-5 as 3.0.0-beta.8".

engram-design commented 2 months ago

Fixed in 3.0.0-beta.9