You are here

public function WebformContentCreatorEntity::createContent in Webform Content Creator 2.x

Create content entity from webform submission.

Parameters

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

Overrides WebformContentCreatorInterface::createContent

File

src/Entity/WebformContentCreatorEntity.php, line 429

Class

WebformContentCreatorEntity
Defines the Webform Content Creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

public function createContent(WebformSubmissionInterface $webform_submission) {
  $entity_type_id = $this
    ->getEntityTypeValue();
  $bundle_id = $this
    ->getBundleValue();
  $fields = WebformContentCreatorUtilities::bundleFields($entity_type_id, $bundle_id);
  if (empty($fields)) {
    return FALSE;
  }

  // Get webform submission data.
  $data = $webform_submission
    ->getData();
  if (empty($data)) {
    return 0;
  }
  $encryptionProfile = $this
    ->getProfileName();
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($entity_type_id);

  // Create new content.
  $content = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id)
    ->create([
    $entity_type
      ->getKey('bundle') => $this
      ->getBundleValue(),
  ]);

  // Set content fields values.
  $attributes = $this
    ->get(WebformContentCreatorInterface::ELEMENTS);
  if (!$this
    ->existsBundle()) {
    return FALSE;
  }
  foreach ($attributes as $k2 => $v2) {
    $content = $this
      ->mapContentField($content, $webform_submission, $fields, $data, $encryptionProfile, $k2, $v2, $attributes);
  }
  $result = FALSE;

  // Save content.
  try {
    $result = $content
      ->save();
  } catch (\Exception $e) {
    \Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
      ->error($this
      ->t('A problem occurred when creating a new content.'));
    \Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
      ->error($e
      ->getMessage());
  }
  return $result;
}