Expand scope to include webforms

Created on 21 February 2022, almost 3 years ago
Updated 4 November 2024, 3 months ago

Problem/Motivation

As webforms are among the most complex node items to build manually, it would be an amazing help to have the option to clone webforms included in this module.
It seems there is currently no module for Drupal 8/9 out there that can do this.

✨ Feature request
Status

Postponed

Version

1.14

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain igerhardt

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bluegeek9
  • Status changed to Closed: works as designed 22 days ago
  • πŸ‡ΊπŸ‡Έ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);
        }
      }
    }
    
Production build 0.71.5 2024