Retrieve mapping values in custom integration #1014
-
|
Hello, As you may know by now I am building a custom integration and making progress :) I am now at the point where I want to map the submission values to the field mapping I made in the form. So the flow I am looking at now is:
I am stuck as I do not see how the Here is the class as I have it now (shortened for readability): <?php
namespace client\modules;
use Craft;
use craft\errors\InvalidFieldException;
use craft\helpers\App;
use Exception;
use fmREST;
use RuntimeException;
use Throwable;
use verbb\formie\base\Crm;
use verbb\formie\base\Integration;
use verbb\formie\elements\Submission;
use verbb\formie\models\IntegrationField;
use verbb\formie\models\IntegrationFormSettings;
class Filemaker extends Crm
{
public string $hostName;
public string $database;
public string $username;
public string $password;
public ?array $contactFieldMapping = null;
public ?array $dealFieldMapping = null;
public ?array $leadFieldMapping = null;
public ?array $companyFieldMapping = null;
public ?array $formFieldMapping = null;
public bool $debug = false;
public static function displayName(): string
{
return Craft::t('formie', 'FileMaker');
}
public function getIconUrl(): string
{
return Craft::$app->getAssetManager()->getPublishedUrl("@client/web/assets/cp/filemaker.svg", true);
}
public function getDescription(): string
{
return Craft::t('formie', 'Provides FileMaker integration.');
}
public function getSettingsHtml(): string
{
return Craft::$app->getView()->renderTemplate('client/integrations/crm/filemaker/_plugin-settings', [
'integration' => $this,
]);
}
public function getFormSettingsHtml($form): string
{
return Craft::$app->getView()->renderTemplate('client/integrations/crm/filemaker/_form-settings', [
'integration' => $this,
'form' => $form
]);
}
public function fetchFormSettings(): IntegrationFormSettings
{
$settings = [];
try
{
if (!Craft::$app->getRequest()->getParam('sendFormPayload')) {
throw new \InvalidArgumentException('A layout must be specified');
}
$settings['syncFormSuccess'] = '';
$settings['syncFormError'] = '';
$layout = Craft::$app->getRequest()->getParam('layout');
if (!$layout) {
$settings['syncFormError'] = Craft::t('formie', 'Layout required');
}
else
{
$response = $this->getFmClient()->layoutMetadata($layout);
if ($response['messages'][0]['code'] > 0)
{
Integration::error($this, $response['messages'][0]['message'], true);
}
$filemakerFields = [];
foreach ($response['response']['fieldMetaData'] as $field)
{
$filemakerFields[] =
new IntegrationField([
'handle' => $field['name'],
'name' => Craft::t('formie', $field['name']),
'required' => false,
]);
}
$settings['syncFormSuccess'] = Craft::t('formie', 'Successfully synced FileMaker fields');
$settings['fields'] = $filemakerFields;
}
} catch (Throwable $e) {
Integration::apiError($this, $e);
}
return new IntegrationFormSettings($settings);
}
public function sendPayload(Submission $submission): bool
{
$formValues = $this->getFieldMappingValues($submission, $this->formFieldMapping, 'fields');
print_r($formValues);
return true;
}
public function fetchConnection(): bool
{
try {
$this->getFmClient()->productInfo();
} catch (Throwable $e) {
Integration::apiError($this, $e);
return false;
}
return true;
}
}In the database I have seen that the field mapping is stored for the form but the code cannot find it for some reason. Where am I missing something? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 12 replies
-
|
So firstly, you should supply settings for the mapping screen for a particular name. You've done this in your return new IntegrationFormSettings([
'fields' => [
new IntegrationField([
'handle' => $field['name'],
'name' => Craft::t('formie', $field['name']),
'required' => false,
]),
]
]);Here, you create a collection of I'd recommend you change this from $settings['form'] = $filemakerFields;Because those fields represent the settings for mapping to a "form object", just like a "contact object", deal, etc. In If you run Again, looking at ActiveCampaign as an example, you should have something like this in your form template. Notice the Can you let me know the template side of things, and maybe adjust from |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Ah yes, the Then run |
Beta Was this translation helpful? Give feedback.
-
|
I have visual 😄 Glad to know it wasn't just me 🙃 Thank you for the fix, I can confirm it works for me. Now I can implement the rest. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
@engram-design I promise I triple-checked that block of code 🙈 Thank you, that does it. One other thing I found out, I have now 2 forms and when I load the fields in form 1 and go to form 2, I see the fields from form 1. I reload the fields and see my form 2 fields but when I then go to form 1, I see the fields from form 2 🙃 Is there some kind of caching setting I have to set? |
Beta Was this translation helpful? Give feedback.
-
|
Hello, sorry for the late reply but I had no access again to the CRM until today. So to explain what I see I have created a short video, this hopefully explains what I mean. As you can see when I switch forms and refresh the fields I get the correct fields but going back to the form I see the fields from the other form. Does this video help? formie-fields.mp4. |
Beta Was this translation helpful? Give feedback.
-
|
@engram-design I figured it out, it is not PHP 8.1 as I am running PHP 8.0 but something else. So when I retrieve the field mapping it is eventually stored in the My question now is, how do I store these fields in a form specific cache? Is that even supported? |
Beta Was this translation helpful? Give feedback.
-
|
Hello @engram-design Apologies for getting back this late but other priorities took over.
I completely follow that. So my question now is, where or what or when is something triggered to fill the This is probably also my problem, nothing is stored in this field. I only see the I am checking it but not finding my golden egg yet :) |
Beta Was this translation helpful? Give feedback.
-
|
@engram-design Thank you for that, it is starting to make some sense now. So in a previous post we renamed the In this field I see the correct fields for the given layout. Up to here all is good.
That is looking good now as I see the name of my integration in the POST request. Now, when rendering the table with fields, I still see the fields from the previous form I was. So somehow it is not taking the fields from the |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
😄 This is a Filemaker connection and in FIlemaker you create a layout per form. So you can map your website form to the Filemaker form.
I actually made those changes as well and that works. Like you said, you will see no fields until I click This workaround is workable for me, as I can map my fields and the mapping can be used when posting the payload. The integration is something I setup only once (most of the time at least) and after that it is set. Thank you for your help in this and hopefully the option to set fields per form in cache can be added to the wishlist. |
Beta Was this translation helpful? Give feedback.







Ah yes, the
run-integrationcommand is actually brand-new, but there's a bug with that. Fixed for the next release. To get this early, change yourverbb/formierequirement incomposer.jsonto:Then run
composer update.