public function WebformContentCreatorEntity::createNode in Webform Content Creator 3.x
Same name and namespace in other branches
- 8 src/Entity/WebformContentCreatorEntity.php \Drupal\webform_content_creator\Entity\WebformContentCreatorEntity::createNode()
Create node from webform submission.
Parameters
\Drupal\webform\WebformSubmissionInterface $webform_submission: Webform submission.
Return value
int Result after saving content.
Overrides WebformContentCreatorInterface::createNode
File
- src/
Entity/ WebformContentCreatorEntity.php, line 428
Class
- WebformContentCreatorEntity
- Defines the Webform Content creator entity.
Namespace
Drupal\webform_content_creator\EntityCode
public function createNode(WebformSubmissionInterface $webform_submission) {
$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);
// Create new node.
$content = Node::create([
WebformContentCreatorInterface::TYPE => $this
->getContentType(),
'title' => $decoded_title,
]);
// Set node fields values.
$attributes = $this
->get(WebformContentCreatorInterface::ELEMENTS);
$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;
}
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 when creating a new node.'));
\Drupal::logger(WebformContentCreatorInterface::WEBFORM_CONTENT_CREATOR)
->error($e
->getMessage());
}
return $result;
}