0

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:

enter image description here

Contact webform element:

enter image description here

Now, after submitting the contact webform:

enter image description here

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:

enter image description here

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();
  }
}

1 Answer 1

0

Finally, I am decided to go with "Business rules" module which will allow to create any content type and transfer all data from webform submission to the created node by setting up a few actions to be triggered on the rule event: entity insert of type webform submission.

See video tutorials here: https://www.youtube.com/channel/UC3jgO5Yt-5y__VVT74qRgSg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.