You are here

private function WebformContentCreatorEntity::mapNodeField in Webform Content Creator 3.x

Same name and namespace in other branches
  1. 8 src/Entity/WebformContentCreatorEntity.php \Drupal\webform_content_creator\Entity\WebformContentCreatorEntity::mapNodeField()

Use a single mapping to set a Node field value.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $initial_content: Content being mapped with a webform submission.

\Drupal\webform\WebformSubmissionInterface $webform_submission: Webform submission entity.

array $fields: Node fields.

array $webform_fields: Webform fields (flattened).

array $data: Webform submission data.

string $encryption_profile: Encryption profile used in Webform encrypt module.

string $field_id: Node field id.

array $mapping: Single mapping between node and webform submissions.

array $attributes: All mapping values between Node and Webform submission values.

Return value

\Drupal\Core\Entity\ContentEntityInterface Created node.

2 calls to WebformContentCreatorEntity::mapNodeField()
WebformContentCreatorEntity::createNode in src/Entity/WebformContentCreatorEntity.php
Create node from webform submission.
WebformContentCreatorEntity::updateNode in src/Entity/WebformContentCreatorEntity.php
Update node from webform submission.

File

src/Entity/WebformContentCreatorEntity.php, line 333

Class

WebformContentCreatorEntity
Defines the Webform Content creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

private function mapNodeField(ContentEntityInterface $initial_content, WebformSubmissionInterface $webform_submission, array $fields = [], array $webform_fields = [], array $data = [], $encryption_profile = '', $field_id = '', array $mapping = [], array $attributes = []) {
  $content = $initial_content;
  $webform = $webform_submission
    ->getWebform();
  if (!$content
    ->hasField($field_id) || !is_array($mapping)) {
    return $content;
  }

  // Get the field mapping plugin.
  $field_mapping = \Drupal::service('plugin.manager.webform_content_creator.field_mapping')
    ->getPlugin($attributes[$field_id][WebformContentCreatorInterface::FIELD_MAPPING]);
  $component_fields = $field_mapping
    ->getEntityComponentFields($fields[$field_id]);
  $values = [];
  $webform_element = [];
  if (sizeOf($component_fields) > 0) {
    foreach ($component_fields as $component_field) {
      $webform_element[$component_field] = $webform
        ->getElement($mapping[$component_field][WebformContentCreatorInterface::WEBFORM_FIELD], FALSE);

      // If the custom check functionality is active then we do need to evaluate
      // webform fields.
      if ($attributes[$field_id][WebformContentCreatorInterface::CUSTOM_CHECK]) {
        $field_value = WebformContentCreatorUtilities::getTokenValue($mapping[WebformContentCreatorInterface::CUSTOM_VALUE], $encryption_profile, $webform_submission);
      }
      else {
        if (!$attributes[$field_id][WebformContentCreatorInterface::TYPE]) {
          if (!array_key_exists(WebformContentCreatorInterface::WEBFORM_FIELD, $mapping) || !array_key_exists($mapping[WebformContentCreatorInterface::WEBFORM_FIELD], $data)) {
            return $content;
          }
          $field_value = $this
            ->getDecryptionFromProfile($data[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]], $encryption_profile);
          if ($fields[$field_id]
            ->getType() === 'entity_reference' && (!is_array($field_value) && intval($field_value) === 0)) {
            $content
              ->set($field_id, []);
            return $content;
          }
        }
        else {
          $field_object = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]};
          if ($field_object instanceof EntityReferenceFieldItemList) {
            $field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}
              ->getValue()[0]['target_id'];
          }
          else {
            $field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}->value;
          }
        }
      }
      $values[$component_field] = $field_value;
    }
  }
  else {

    // If the custom check functionality is active then we do need to evaluate
    // webform fields.
    if ($attributes[$field_id][WebformContentCreatorInterface::CUSTOM_CHECK]) {
      $field_value = WebformContentCreatorUtilities::getTokenValue($mapping[WebformContentCreatorInterface::CUSTOM_VALUE], $encryption_profile, $webform_submission);
    }
    else {
      if (!$attributes[$field_id][WebformContentCreatorInterface::TYPE]) {
        if (!array_key_exists(WebformContentCreatorInterface::WEBFORM_FIELD, $mapping) || !array_key_exists($mapping[WebformContentCreatorInterface::WEBFORM_FIELD], $data)) {
          return $content;
        }
        $field_value = $this
          ->getDecryptionFromProfile($data[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]], $encryption_profile);
        if ($fields[$field_id]
          ->getType() === 'entity_reference' && (!is_array($field_value) && intval($field_value) === 0)) {
          $content
            ->set($field_id, []);
          return $content;
        }
      }
      else {
        $field_object = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]};
        if ($field_object instanceof EntityReferenceFieldItemList) {
          $field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}
            ->getValue()[0]['target_id'];
        }
        else {
          $field_value = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}->value;
        }
      }
    }
    $values[$field_id] = $field_value;
  }
  if ($fields[$field_id]
    ->getType() == 'datetime') {
    $field_value = $this
      ->convertTimestamp($fields, $field_id, $field_value);
  }

  // Map the field type using the selected field mapping.
  $field_value = $field_mapping
    ->mapEntityField($content, $webform_element, $values, $fields[$field_id]);
  return $content;
}