public function WebformContentCreatorEntity::createNode in Webform Content Creator 8
Same name and namespace in other branches
- 3.x src/Entity/WebformContentCreatorEntity.php \Drupal\webform_content_creator\Entity\WebformContentCreatorEntity::createNode()
Create node from webform submission.
Parameters
\Drupal\webform\WebformSubmissionInterface $webform_submission: Webform submission.
Overrides WebformContentCreatorInterface::createNode
File
- src/
Entity/ WebformContentCreatorEntity.php, line 387
Class
- WebformContentCreatorEntity
- Defines the Webform Content creator entity.
Namespace
Drupal\webform_content_creator\EntityCode
public function createNode(WebformSubmissionInterface $webform_submission) {
$nodeTitle = $this
->getNodeTitle();
// Get webform submission data.
$data = $webform_submission
->getData();
if (empty($data)) {
return 0;
}
$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);
// Create new node.
$content = Node::create([
WebformContentCreatorInterface::TYPE => $this
->getContentType(),
'title' => $decodedTitle,
]);
// Set node fields values.
$attributes = $this
->get(WebformContentCreatorInterface::ELEMENTS);
$contentType = \Drupal::entityTypeManager()
->getStorage('node_type')
->load($this
->getContentType());
if (empty($contentType)) {
return FALSE;
}
$fields = WebformContentCreatorUtilities::contentTypeFields($contentType);
if (empty($fields)) {
return FALSE;
}
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 when creating a new node.'));
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($e
->getMessage());
}
return $result;
}