0

I have found this question here to fit my exact needs since the rules modules is not ready yet

How to create a node out of a webform submission?

I am hardly a coder, can someone please explain where i should use place this code in my Drupal 8 website ?

MY USE CASE:

1) Content type (xyz) with below fields:

  • field_xyz_id (default Title field)

  • field_xyz_name (Text Field)

  • field_xyz_telephone (Telephone field)

  • field_xyz_category (Taxonomy reference field)

  • field_xyz_company (Group reference field)

  • field_xyz_user (User reference field)

  • field_xyz_color (Color field)

  • field_xyz_image (multi value image field)

2) Webform (registerxyz) with below fields:

  • form_registerxyz_name (Text Field)

  • form_registerxyz_telephone (Telephone field)

  • form_registerxyz_category (Taxonomy reference field)

  • form_registerxyz_company (Group reference field)

  • form_registerxyz_user (User reference field)

  • form_registerxyz_color (Color field)

  • form_registerxyz_image (multi value image field)

When a registered user with role manager submit the webform regisyer xyz, a new node of type xyz will be created and the webform submission value to be transfered to the node's fields.

Back in D7: I used to use the webform rules to accomplish the above but until today both rules and webform rules are not ready yet

Any help in where i can place the above code in my drupal 8 website ?

I am open to any other suggestions (if any) to create a node out of webform submissions !

UPDATE # 1:

I have create a custom module (my_module_name) and inside \my_module_name\src\Plugin\WebformHandler\ExampleWebformHandler.php i have placed the below 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.
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

  /** @var Node $node */   
   $node = Node::create([
    // node type
        'type' => 'article',
    // mandatory fields
        'title' => $webform_submission->getData('subject'),
    // optional fields
        'body' => $webform_submission->getData('message'),
    ]);
    $node->save();

  }
}

I also under the settings of the default built-in contact webform, i have added the above handler.

Now, after i create a new Webform submission, the below errors appear in the recent log messages section:

FIRST ERROR:

Notice: Undefined variable: webformSubmission in Drupal\my_module_name\Plugin\WebformHandler\ExampleWebformHandler->submitForm() (line 45 of S:\Setup Ideas Sites\drupal8\modules\Custom\my_module_name\src\Plugin\WebformHandler\ExampleWebformHandler.php) #0 S:\Setup Ideas Sites\drupal8\core\includes\bootstrap.inc(582): _drupal_error_handler_real(8, 'Undefined varia...', 'S:\\Setup Ideas ...', 45, Array) #1 S:\Setup Ideas Sites\drupal8\modules\Custom\my_module_name\src\Plugin\WebformHandler\ExampleWebformHandler.php(45): _drupal_error_handler(8, 'Undefined varia...', 'S:\\Setup Ideas ...', 45, Array) #2 S:\Setup Ideas Sites\drupal8\modules\Contrib\webform-8.x-5.x-dev\webform\src\Entity\Webform.php(2250): Drupal\my_module_name\Plugin\WebformHandler\ExampleWebformHandler->submitForm(Array, Object(Drupal\Core\Form\FormState), Object(Drupal\webform\Entity\WebformSubmission)) #3 S:\Setup Ideas Sites\drupal8\modules\Contrib\webform-8.x-5.x-dev\webform\src\WebformSubmissionForm.php(1441): Drupal\webform\Entity\Webform->invokeHandlers('submitForm', Array, Object(Drupal\Core\Form\FormState), Object(Drupal\webform\Entity\WebformSubmission)) #4 [internal function]: Drupal\webform\WebformSubmissionForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #5 S:\Setup Ideas Sites\drupal8\core\lib\Drupal\Core\Form\FormSubmitter.php(111): call_user_func_array(Array, Array) #6...

SECOND ERROR:

Error: Call to a member function getData() on null in Drupal\my_module_name\Plugin\WebformHandler\ExampleWebformHandler->submitForm() (line 45 of S:\Setup Ideas Sites\drupal8\modules\Custom\my_module_name\src\Plugin\WebformHandler\ExampleWebformHandler.php) #0 S:\Setup Ideas Sites\drupal8\modules\Contrib\webform-8.x-5.x-dev\webform\src\Entity\Webform.php(2250): Drupal\my_module_name\Plugin\WebformHandler\ExampleWebformHandler->submitForm(Array, Object(Drupal\Core\Form\FormState), Object(Drupal\webform\Entity\WebformSubmission)) #1 S:\Setup Ideas Sites\drupal8\modules\Contrib\webform-8.x-5.x-dev\webform\src\WebformSubmissionForm.php(1441): Drupal\webform\Entity\Webform->invokeHandlers('submitForm', Array, Object(Drupal\Core\Form\FormState), Object(Drupal\webform\Entity\WebformSubmission)) #2 [internal function]: Drupal\webform\WebformSubmissionForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #3 S:\Setup Ideas Sites\drupal8\core\lib\Drupal\Core\Form\FormSubmitter.php(111): call_user_func_array(Array, Array) #4...

Any help please ? look like i have to define some variables to hold the webform submissions !??

UPDATE #2

After fixing the S in the above code, and trying again to submit the contact webform, two new errors are now displayed:

Error Nbr1:

Error Nbr 1

Error Nbr 2:

Error Nbr 2

Your help is much appreciated, thank you

closed as too broad by kiamlaluno Jun 1 at 6:43

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It's WebformSubmissionInterface $webform_submission so you need to call it the same inside the function $webform_submission->getData() – theuni Jul 15 '18 at 15:49
  • @theuni I updated my code as you mentioned in your last comment but still got the same error !!! FYI, the error message pointing that the error is at line 45 in the php code which is the line starting with the 'title' – user86221 Jul 19 '18 at 2:23
  • 1
    Again, it's $webform_submission->getData(). You wrote $webform_Submission->getData() with a uppercase S – theuni Jul 24 '18 at 8:04
  • 1
    @theuni excuse my ignorance about the sensitivity of the php language code, however, after fixing the "S" issue i got two new error now... can you please check them out in the UPDATE #2 secrion (if you have time)... thank you – user86221 Jul 25 '18 at 16:11
  • 1
    There could be many possible errors. Does the webform you built have a field (key) called 'title' that you access with $webform_submission->getData('title')? Try to go step by step from creating the webform to accessing the submission data inside the submitForm() function etc. Also for debuggin use kint($webform_submission) inside your submitForm() function to see what keys are available in $webform_submission and what values they have. – theuni Jul 27 '18 at 6:51
1

As that link suggests, you can solve this problem by creating your own custom (webform submission) handler. So create a custom module (the easiest way is probably to use the drupal console and run drupal generate:module in a terminal from your website's root folder). Besides all the necessary files for your module (like *.info.yml etc.) create a file named your-module-name/src/Plugin/WebformHandler/CustomWebformHandler.php. This is where your put all your logic of reading the webform submitted fields and saving them as a node.

As an additional help see this link for an example of how to create a user after a webform submission (the function createUser() is where the node-creation logic is in this example).

Update To create your content type just write your logic similar to this code-draft and read more about custom webformhandlers.

class CustomWebformHandler extends WebformHandlerBase {
  ...
  // Create node object from webform-submission.
  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'        => 'type-of-your-content',
      'field_xyz_name' => $values['name'],
      'field_xyz_telephone' => $values['telephone'],
      ....
    ]);
    // 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();
  }
}

Clear your cache.

Then in your webform's settings-general (UI) check "Disable saving of submissions".

Then under settings-email/handlers add your CustomWebformHandler as a new submission handler.

These are the basic steps that should help you move on.

  • thank you for your kind help, i will sure try the above explanation. I have one question for clarification: 1) the php code in that link above is what will go inside the customwebformhandler.php file right ? – user86221 Jul 9 '18 at 12:01
  • Yes, modified accordingly based on your logic of course. – theuni Jul 9 '18 at 12:16
  • can you please check the update to my question ! – user86221 Jul 14 '18 at 16:49
  • Again, it's $webform_submission->getData(). You wrote $webform_Submission->getData() with a uppercase S – theuni Jul 24 '18 at 8:11
  • The answer is correct but you should use WebformSubmissionInterface in your function instead of Webform_submissionInterface because this is what being called in the update section of the question above. – Charbel El Azzi Aug 6 '18 at 0:49