public function WebformContentCreatorEntity::updateNode in Webform Content Creator 3.x
Same name and namespace in other branches
- 8 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
int Result after saving content.
Overrides WebformContentCreatorInterface::updateNode
File
- src/
Entity/ WebformContentCreatorEntity.php, line 498
Class
- WebformContentCreatorEntity
- Defines the Webform Content creator entity.
Namespace
Drupal\webform_content_creator\EntityCode
public function updateNode(WebformSubmissionInterface $webform_submission, $op = 'edit') {
if (empty($this
->getSyncContentField())) {
return 0;
}
$content_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load($this
->getContentType());
if (empty($content_type)) {
return 0;
}
// Get the webform fields flattened to identify field types.
$webform = $webform_submission
->getWebform();
$webform_fields = [];
if ($webform) {
$webform_fields = $webform
->getElementsDecodedAndFlattened();
}
$fields = WebformContentCreatorUtilities::contentTypeFields($content_type);
if (empty($fields)) {
return 0;
}
if (!array_key_exists($this
->getSyncContentField(), $fields)) {
return 0;
}
$node_title = $this
->getNodeTitle();
// Get webform submission data.
$data = $webform_submission
->getData();
if (empty($data)) {
return 0;
}
$encryption_profile = $this
->getProfileName();
// Get title.
$title = WebformContentCreatorUtilities::getTokenValue($node_title, $encryption_profile, $webform_submission);
// Decode HTML entities, returning them to their original UTF-8 characters.
$decoded_title = Html::decodeEntities($title);
// 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 0;
}
if ($op === 'delete' && !empty($this
->getSyncDeleteContentCheck())) {
$result = $content
->delete();
return $result;
}
if (empty($this
->getSyncEditContentCheck())) {
return 0;
}
// Set title.
$content
->setTitle($decoded_title);
// Set node fields values.
$attributes = $this
->get(WebformContentCreatorInterface::ELEMENTS);
foreach ($attributes as $k2 => $v2) {
$content = $this
->mapNodeField($content, $webform_submission, $fields, $webform_fields, $data, $encryption_profile, $k2, $v2, $attributes);
}
$result = 0;
// 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;
}