- Issue created by @alexharries
- last update
almost 2 years ago 3 pass
Hello folks,
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.
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.
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
Needs review
1.0
Code