You are here

public function AddressFieldMapping::mapEntityField in Webform Content Creator 3.x

Use a single mapping to set an entity field value.

Overrides FieldMappingBase::mapEntityField

File

src/Plugin/WebformContentCreator/FieldMapping/AddressFieldMapping.php, line 33

Class

AddressFieldMapping
Provides an address field mapping.

Namespace

Drupal\webform_content_creator\Plugin\WebformContentCreator\FieldMapping

Code

public function mapEntityField(ContentEntityInterface &$content, array $webform_element, array $data = [], FieldDefinitionInterface $field_definition) {
  $field_id = $field_definition
    ->getName();
  $field_value = $field_definition
    ->getDefaultValue($content)[0];
  if ($webform_element[$field_id]['#type'] == "webform_address") {
    if (!empty($data[$field_id]['address'])) {
      $field_value['address_line1'] = $data[$field_id]['address'];
    }
    if (!empty($data[$field_id]['address_2'])) {
      $field_value['address_line2'] = $data[$field_id]['address_2'];
    }
    if (!empty($data[$field_id]['city'])) {
      $field_value['locality'] = $data[$field_id]['city'];
    }
    if (!empty($data[$field_id]['state_province'])) {
      $field_value['administrative_area'] = $data[$field_id]['state_province'];
    }
    if (!empty($data[$field_id]['postal_code'])) {
      $field_value['postal_code'] = $data[$field_id]['postal_code'];
    }
    if (!empty($data[$field_id]['country'])) {
      $field_value['country_code'] = $data[$field_id]['country'];
    }
  }
  else {
    $field_value = array_merge($field_value, $data[$field_id]);
  }
  $content
    ->set($field_id, $field_value);
}