logo

Programatically submit data into Webform in Drupal 8

05
September

Programatically submit data into Webform in Drupal 8
By: Anonymous | Published On: Sun, 09/05/2021 - 22:35

Folowing code you can use to save data in webform in Drupal 8

In following code used webform id as "contact_us",

		$webform_id = 'contact_us';	// here use can use that webform id where to save data
		$webform = Webform::load($webform_id);
		// Create webform submission.
		$values = [
		'webform_id' => $webform->id(),
		'data' => [
		'name' => $name,
		'phone' => $phone,
		'email' => $email,
		],
		];

		/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
		$webform_submission = WebformSubmission::create($values);
		$webform_submission->save(); 
				

You need to add these code in header

use Drupal\webform\Entity\Webform;
use Drupal\webform\WebformSubmissionForm;
use Drupal\webform\Entity\WebformSubmission;

Need Help ?