- Status changed to Closed: works as designed
22 days ago 7:21pm 13 January 2025 - πΊπΈUnited States bluegeek9
Webforms are not nodes. This project only works with nodes.
- π³πΏNew Zealand davidwhthomas
This approach worked for me, for others looking:
/** * Alters a node when it is cloned. * * @param \Drupal\node\NodeInterface $node * The node being cloned. */ function MY_MODULE_cloned_node_alter(NodeInterface &$node) { // Check if the node has a 'webform' field and it's not empty. if ($node->hasField('webform') && !$node->webform->isEmpty()) { // Load the original webform. $webform_id = $node->get('webform')->target_id; $webform = \Drupal::entityTypeManager()->getStorage('webform')->load($webform_id); if ($webform) { /** @var \Drupal\webform\WebformInterface $duplicate */ $duplicate = $webform->createDuplicate(); // Set the new webform's ID and title. $duplicate->set('id', 'webform_' . uniqid()); $duplicate->set('title', 'Clone of ' . $webform->label()); // Note: The duplicate webform is saved before updating the node. $duplicate->save(); // Associate the duplicated webform with the cloned node. // The node is saved later. $node->set('webform', $duplicate); } } }