Skip to content
Snippets Groups Projects
Commit f8823408 authored by jensschuppe's avatar jensschuppe
Browse files

Issue #3341602: Add configuration for how to format field values when sending to CiviCRM

parent 30ed4870
No related branches found
No related tags found
1 merge request!6Issue #3341602: Add configuration for how to format field values when sending to CiviCRM
......@@ -8,8 +8,10 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\file\Entity\File;
use Drupal\webform\Plugin\WebformElement\BooleanBase;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformOptionsHelper;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformTokenManagerInterface;
......@@ -172,7 +174,31 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
'With Yes selected, saving this configuration will synchronise the Webform with the Form Processor input fields, adding (not updating) checked fields to and deleting unchecked fields from the Webform.'
),
];
// Add a table of form processor fields currently in the form for
// defining their format to be used for submissions to CiviCRM.
$form['additional']['submission_settings'] = [
'#type' => 'fieldset',
'#title' => $this->t('Submission settings'),
'fields' => [
'#type' => 'table',
'#header' => [
'field' => ['data' => $this->t('Field'), 'width' => '50%'],
'format' => [
'data' => $this->t('Format'),
'class' => [RESPONSIVE_PRIORITY_LOW],
'width' => '50%',
],
],
'#sticky' => TRUE,
],
];
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
foreach ($fpFields as $key => $field) {
// Add checkboxes for adding/deleting form processor fields to/from
// the form when saving configuration.
$form['additional']['fields']['field'][$key] = [
'#type' => 'checkbox',
'#title' => $field,
......@@ -185,6 +211,33 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
]
]
];
// Add configuration option for selecting the format to use for
// sending field values to CiviCRM.
if ($element = $this->getWebform()->getElement($key)) {
$webform_element = $element_manager->getElementInstance($element);
$form['additional']['submission_settings']['fields'][$key]['field'] = [
'#markup' => $element['#title'],
];
$form['additional']['submission_settings']['fields'][$key]['format'] = [
'#type' => 'select',
'#title' => $this->t('Item format'),
'#description' => $this->t('Select how a single value is submitted to CiviCRM.'),
'#options' => WebformOptionsHelper::appendValueToText($webform_element->getItemFormats()),
// Default value for unconfigured elements with options or of a
// Boolean type should be "Raw value" for sending option keys
// instead of labels.
'#default_value' =>
$this->configuration['submission_settings']['fields'][$key]['format']
?? (
(
$webform_element->hasProperty('options')
|| is_a($webform_element, BooleanBase::class)
) ? 'raw'
: NULL
),
];
}
}
}
......@@ -284,6 +337,7 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
$builder->save();
}
$this->configuration['form_processor_update_fields'] = $values['additional']['fields']['update_fields'];
$this->configuration['submission_settings'] = $values['additional']['submission_settings'];
}
private function getContactId() {
......@@ -401,6 +455,24 @@ class FormProcessorWebformHandler extends WebformHandlerBase {
];
$params[$key] = $fileData;
}
elseif ($this->configuration['submission_settings']['fields'][$key]['format']) {
// Use the selected format from configuration for rendering the field
// value. We're using Webform's buildExportRecord() method for
// rendering, as it takes care of element-specific formatting, such as
// deciding whether to render as HTML or text.
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
$element = $this->getWebform()->getElement($key);
$element['#format'] = $this->configuration['submission_settings']['fields'][$key]['format'];
$export_record = $element_manager
->getElementInstance($element)
->buildExportRecord(
$element,
$webform_submission,
['webform' => $this->getWebform()]
);
$params[$key] = (string) reset($export_record);
}
else {
$params[$key] = $data[$key];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment