You are here

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

Update content 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::updateContent

File

src/Entity/WebformContentCreatorEntity.php, line 488

Class

WebformContentCreatorEntity
Defines the Webform Content Creator entity.

Namespace

Drupal\webform_content_creator\Entity

Code

public function updateContent(WebformSubmissionInterface $webform_submission, $op = 'edit') {
  if (empty($this
    ->getSyncContentField())) {
    return FALSE;
  }
  $entity_type_id = $this
    ->getEntityTypeValue();
  $bundle_id = $this
    ->getBundleValue();
  $fields = WebformContentCreatorUtilities::bundleFields($entity_type_id, $bundle_id);
  if (empty($fields)) {
    return FALSE;
  }
  if (!array_key_exists($this
    ->getSyncContentField(), $fields)) {
    return FALSE;
  }

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

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

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

  // Set content fields values.
  $attributes = $this
    ->get(WebformContentCreatorInterface::ELEMENTS);
  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 while updating content.'));
    \Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
      ->error($e
      ->getMessage());
  }
  return $result;
}