You are here

public function WebformContentCreatorEntity::updateNode 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::updateNode()

Update node from webform submission.

Parameters

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

string $op: Operation.

Return value

bool True, if succeeded. Otherwise, return false.

Overrides WebformContentCreatorInterface::updateNode

File

src/Entity/WebformContentCreatorEntity.php, line 450

Class

WebformContentCreatorEntity
Defines the Webform Content creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

public function updateNode(WebformSubmissionInterface $webform_submission, $op = 'edit') {
  if (empty($this
    ->getSyncContentField())) {
    return FALSE;
  }
  $contentType = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->load($this
    ->getContentType());
  if (empty($contentType)) {
    return FALSE;
  }
  $fields = WebformContentCreatorUtilities::contentTypeFields($contentType);
  if (empty($fields)) {
    return FALSE;
  }
  if (!array_key_exists($this
    ->getSyncContentField(), $fields)) {
    return FALSE;
  }
  $nodeTitle = $this
    ->getNodeTitle();

  // Get webform submission data.
  $data = $webform_submission
    ->getData();
  if (empty($data)) {
    return FALSE;
  }
  $encryptionProfile = $this
    ->getProfileName();

  // Decrypt title.
  $decryptedTitle = WebformContentCreatorUtilities::getDecryptedTokenValue($nodeTitle, $encryptionProfile, $webform_submission);

  // Decode HTML entities, returning them to their original UTF-8 characters.
  $decodedTitle = Html::decodeEntities($decryptedTitle);

  // Get nodes created from this webform submission.
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    $this
      ->getSyncContentField() => $webform_submission
      ->id(),
  ]);

  // Use only first result, if exists.
  if (!($content = reset($nodes))) {
    return FALSE;
  }
  if ($op === 'delete' && !empty($this
    ->getSyncDeleteContentCheck())) {
    $result = $content
      ->delete();
    return $result;
  }
  if (empty($this
    ->getSyncEditContentCheck())) {
    return FALSE;
  }

  // Set title.
  $content
    ->setTitle($decodedTitle);

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

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