How to automatically create a Webform node when adding a Webform
Last updated on
27 August 2020
The below code snippet shows to automatically create a Webform node when adding a new Webform.
use Drupal\webform\WebformInterface;
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function CUSTOM_MODULE_webform_insert(WebformInterface $webform) {
$node = \Drupal\node\entity\Node::create([
'type' => 'webform',
'title' => $webform->label(),
'webform' => ['target_id' => $webform->id()],
]);
$node->save();
\Drupal::messenger()->addMessage(t('Node %label created.', ['%label' => $node->label()]));
}
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion