You are here

private function WebformContentCreatorEntity::mapNodeField in Webform Content Creator 8

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

Use a single mapping to set a Node field value.

Parameters

\Drupal\node\NodeInterface $initialContent: Content being mapped with a webform submission.

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

array $fields: Node fields.

array $data: Webform submission data.

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

string $fieldId: 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\node\NodeInterface 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 332

Class

WebformContentCreatorEntity
Defines the Webform Content creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

private function mapNodeField(NodeInterface $initialContent, WebformSubmissionInterface $webform_submission, array $fields = [], array $data = [], $encryptionProfile = '', $fieldId = '', array $mapping = [], array $attributes = []) {
  $content = $initialContent;
  if (!$content
    ->hasField($fieldId) || !is_array($mapping)) {
    return $content;
  }
  if ($attributes[$fieldId][WebformContentCreatorInterface::CUSTOM_CHECK]) {
    $decValue = WebformContentCreatorUtilities::getDecryptedTokenValue($mapping[WebformContentCreatorInterface::CUSTOM_VALUE], $encryptionProfile, $webform_submission);
    if ($decValue === 'true' || $decValue === 'TRUE') {
      $decValue = TRUE;
    }
  }
  else {
    if (!$attributes[$fieldId][WebformContentCreatorInterface::TYPE]) {
      if (!array_key_exists(WebformContentCreatorInterface::WEBFORM_FIELD, $mapping) || !array_key_exists($mapping[WebformContentCreatorInterface::WEBFORM_FIELD], $data)) {
        return $content;
      }
      $decValue = $this
        ->getDecryptionFromProfile($data[$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]], $encryptionProfile);
      if ($fields[$fieldId]
        ->getType() === 'entity_reference' && (!is_array($decValue) && intval($decValue) === 0)) {
        $content
          ->set($fieldId, []);
        return $content;
      }
    }
    else {
      $fieldObject = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]};
      if ($fieldObject instanceof EntityReferenceFieldItemList) {
        $decValue = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}
          ->getValue()[0]['target_id'];
      }
      else {
        $decValue = $webform_submission->{$mapping[WebformContentCreatorInterface::WEBFORM_FIELD]}->value;
      }
    }
  }
  if ($fields[$fieldId]
    ->getType() == 'datetime') {
    $decValue = $this
      ->convertTimestamp($decValue, $fields, $fieldId);
  }

  // Check if field's max length is exceeded.
  $maxLength = $this
    ->checkMaxFieldSizeExceeded($fields, $fieldId, $decValue);
  if ($maxLength === 0) {
    $content
      ->set($fieldId, $decValue);
  }
  else {
    $content
      ->set($fieldId, substr($decValue, 0, $maxLength));
  }
  return $content;
}