Problem/Motivation

When building Webform integrations in other modules it can be useful to have a simple way of mapping a set of data against the current webform.

Proposed resolution

Provide a new field element that extends WebformMapping with the source predefined to show the list of available fields on this webform.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DamienMcKenna created an issue. See original summary.

jrockowitz’s picture

@damienmckenna Yep the WebformMapping element could be extended to create a WebformElementsMapping.

My long-term dream is to have a WebformHandler that maps a webform's submission data to any entity's fields. There is even a prototype of the UI.

DamienMcKenna’s picture

Status: Active » Needs review
FileSize
2.22 KB

WIP.

DamienMcKenna’s picture

Minor tweaks. Just need to settle on a class name and then tests?

jrockowitz’s picture

+++ b/src/Element/WebformValueMapping.php
@@ -0,0 +1,77 @@
+    // Get all of the custom fields on this Webform.
+    $webform = $this->getWebform();
+    $elements = $webform->getElementsInitializedFlattenedAndHasValue('view');
+    foreach ($elements as $key => $element) {
+      $fields[$key] = $this->t("@title (%type)", [
+          '@title' => ($element['#admin_title'] ?: $element['#title'] ?: $key),
+          '%type' => (isset($element['#type']) ? $element['#type'] : ''),
+        ]);
+    }
+
+    // List all of the fields on the base entity.
+    $submission_storage = \Drupal::entityTypeManager()
+      ->getStorage('webform_submission');
+    $field_definitions = $submission_storage->getFieldDefinitions();
+    $field_definitions = $submission_storage->checkFieldDefinitionAccess($webform, $field_definitions);
+    foreach ($field_definitions as $key => $field_definition) {
+      $fields[$key] = $this->t("@title (%type)", [
+          '@title' => $field_definition['title'],
+          '%type' => $field_definition['type'],
+        ]);
+    }

If a field definition key matches an element key, it will overwrite the element's definition. The existing WebformExcludedColumns element has the same problem.

The solution I have used in other places is to prefix element keys with 'element__' in \Drupal\webform\Plugin\WebformElementBase::getTableColumn.

We probably need to dedicated ticket to fix the excluded columns issues.


Maybe the new element's name should WebformSubmissionMapping?

This would allow the WebformSubmissionMapping element to be extended to create a WebformSubmissionToMyCrmMapping element.

DamienMcKenna’s picture

Status: Needs review » Needs work
DamienMcKenna’s picture

Also, the use of $this in listAllFields() breaks OOP because processWebformMapping() is static.

jrockowitz’s picture

Status: Needs work » Postponed (maintainer needs more info)

@DamienMcKenna I am not sure this type of element is immediately needed in the Webform (core) module maybe this code can be developed as an experimental module.

jrockowitz’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)