In drupal 8,
I have created a custom webform handler to create a new article node and copy the data and images from the webform element to the created article node.
In both artcle node and contact webform, i have created the required fields/elements.
Article node fields:
Contact webform element:
Now, after submitting the contact webform:
everything goes as expected and a new Article node is created with all fields filled in with data/images.
However, only the Webform element image select is displaying another image instead of the one selected while submitting the webform.
Below is the created article:
As you can see, the webform element image has been properly populated but the problem is within the webform element image select !!!
I am sure that my custom code is fine as all other fields are populated correctly.
Any idea/help why the picture is being replaced by another one ??!!
Below is my custom handler code:
<?php
namespace Drupal\my_module_name\Plugin\WebformHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;
/**
* Create a new node entity from a webform submission.
*
* @WebformHandler(
* id = "Create a node",
* label = @Translation("Create a node"),
* category = @Translation("Entity Creation"),
* description = @Translation("Creates a new node from Webform Submissions."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class ExampleWebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
// Create node object from webform-submission.
// Function to be fired while submitting the Webform.
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
// Get an array of the values from the submission.
$values = $webform_submission->getData();
// Before you actually save the node, make sure you get what you
// want. So at first comment the line further down with
// $node->save(), run the debug code, check the output and then
// uncomment the $node->save() and delete the debug foreach loop.
// You can use whatever debug method you prefer, this is just a
// simple one-time example.
foreach($values as $key=>$value) {
drupal_set_message($key . ' -> ' . $value);
}
// This is the node creating/saving part.
$node = Node::create([
'type' => 'article',
// Use this format: 'node_machine_name_field' => $values('webform_machine_name_field') for below fields.
'title' => $values['subject'],
'body' => $values['message'],
'field_image' => $values['picture'],
'field_picture_select' => $values['picture_select'],
// etc...
]);
// Before you un-comment this line, make sure you get all the
// values you need from the webform data (in the foreach loop).
$node->save();
}
}