It's similar to issue #3163207 but I need the same functionality for 2.x version.

The problem is when you want to prepopulate for example address fields from webform to content, it's impossible right now. The alteration hook and custom module would solve this problem.

CommentFileSizeAuthor
#3 issue-3230128.patch1.39 KBlobodakyrylo

Comments

lobodacyril created an issue. See original summary.

lobodakyrylo’s picture

Issue summary: View changes
lobodakyrylo’s picture

StatusFileSize
new1.39 KB

The patch adds an alteration hook like this (it's an analogue of the hook presented in #3163207 issue):

use Drupal\webform_content_creator\WebformContentCreatorInterface;

/**
 * Implements hook_webform_content_creator_field_map_value_alter().
 */
function mymodule_webform_content_creator_field_map_value_alter(&$value, array $context) {
  $mapping = $context['mapping'];
  $webform_submission = $context['webform_submission'];
  $webform = $webform_submission ? $webform_submission->getWebform() : NULL;
  $webform_fields = $webform ? $webform->getElementsDecodedAndFlattened() : [];
  $source_type = isset($webform_fields[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]]['#type']) ? $webform_fields[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]]['#type'] : NULL;
  if ($source_type === 'webform_contact') {
    $fields_to_remap = [
      'country' => 'country_code',
      'address' => 'address_line1',
      'address_2' => 'address_line2',
      'city' => 'locality',
      'region' => 'administrative_area',
    ];
    foreach ($fields_to_remap as $old_field => $new_field) {
      if (!isset($value[$old_field])) {
        continue;
      }
      $value[$new_field] = $value[$old_field];
      unset($value[$old_field]);
    }
  }
}
hardik_patel_12’s picture

Instead of the Basic address field, I have used the Advance address field in the webform and mapped the Advance address field with the content type's address field in webform content creator configurations and it's working fine in my case.

joaomarques736’s picture

Hello lobodacyril,

Now we have a new release (3.0.0-alpha2) which already has a plugin system allowing to create mappings for any field types. The address type is already supported.

Let me know if it is ok for you.

Thanks!

joaomarques736’s picture

Version: 2.x-dev » 3.0.0-alpha2
lobodakyrylo’s picture

Status: Needs review » Closed (works as designed)

@joaomarques736, thank you, it works in 3.0.