Embedded Webforms (e.g. in Paragraphs pages) are incorrectly embedded into Clone pages

Created on 3 August 2023, almost 2 years ago

Hello folks,

Problem/Motivation

When using Quick Node Clone on a page which includes an embedded Webform - e.g. a Paragraphs page - the webform is incorrectly built and shown in the clone form because the Webform module doesn't recognise the Clone route name as an edit page.

Normally, on an add or edit route, the webform isn't built but instead the message "[Webform] webform can not be previewed when editing content." is shown:

This check is performed in webform/src/Plugin/Field/FieldFormatter/WebformEntityReferenceEntityFormatter.php:

    $is_entity_add_form = preg_match('/\.add$/', $route_name);

    $is_paragraph = ($items_entity && $items_entity->getEntityTypeId() === 'paragraph');

    // <snip>

    $elements = [];
    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
      if ($is_paragraph_entity_edit_form || $is_paragraph_entity_add_form) {
        // Webform can not be nested within node edit form because the nested
        // <form> tags will cause unexpected validation issues.
        $elements[$delta] = [
          '#type' => 'webform_message',
          '#message_message' => $this->t('%label webform can not be previewed when editing content.', ['%label' => $entity->label()]),
          '#message_type' => 'info',
        ];
      }

This is problematic because you end up with a form inside a form which generally breaks the form altogether. Issue #3259768 "Error when cloning a content with attached webform" seems to be caused by the same issue.

Steps to reproduce

Create a content type with a Paragraph field.

Add a Webform Paragraph and embed a Paragraph.

Create a node with an embedded Webform.

Attempt to clone the node.

Proposed resolution

While it would be nice to be able to have the Webform module recognise the Clone route name, that would require involvement from the maintainers of Webform and such help might take some time to agree.

A simpler workaround is to change the Clone route name to include ".add", as the Webform module will correctly identify that the form is being built for an edit/add page, and will then not embed the Webform in the node add form.

We also need to implement a hook_gin_content_form_routes_alter() to let the Gin theme know about the new Clone route:

/**
 * Implements hook_gin_content_form_routes_alter().
 */
function quick_node_clone_gin_content_form_routes_alter(array &$routes) {
  // Add the new .add route for Gin.
  $routes[] = 'quick_node_clone.node.quick_clone.add';
}

This is because the following code in gin/src/GinContentFormHelper.php will no longer correctly spot the Clone route name:

  public function isContentForm(array $form = NULL, FormStateInterface $form_state = NULL, $form_id = '') {
    $is_content_form = FALSE;

    // Get route name.
    $route_name = $this->routeMatch->getRouteName();

    // Routes to include.
    $route_names = [
      'node.add',
      'entity.node.content_translation_add',
      'entity.node.content_translation_edit',
      'quick_node_clone.node.quick_clone',
      'entity.node.edit_form',
    ];

The attached patch (below) implements this change.

Apologies for no test coverage - if anyone thinks this patch is useful I'd be grateful for any help writing test coverage :)

Best wishes

Alex

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom alexharries

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

Comments & Activities

Production build 0.71.5 2024