You are here

private function WebformContentCreatorEntity::mapContentField in Webform Content Creator 2.x

Use a single mapping to set a Content field value.

Parameters

\Drupal\Core\Entity\EntityInterface $initialContent: Content entity being mapped with a webform submission.

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

array $fields: Content entity fields.

array $data: Webform submission data.

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

string $fieldId: Content field id.

array $mapping: Single mapping between content entity and webform submission.

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

Return value

\Drupal\Core\Entity\EntityInterface Created content entity.

2 calls to WebformContentCreatorEntity::mapContentField()
WebformContentCreatorEntity::createContent in src/Entity/WebformContentCreatorEntity.php
Create content entity from webform submission.
WebformContentCreatorEntity::updateContent in src/Entity/WebformContentCreatorEntity.php
Update content from webform submission.

File

src/Entity/WebformContentCreatorEntity.php, line 374

Class

WebformContentCreatorEntity
Defines the Webform Content Creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

private function mapContentField(EntityInterface $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;
}