It is possible to exclude fields from cloning (which is great) but it would be handy if the module could be configured to use default values set for the content type if a field is excluded.

Comments

Sissonen created an issue. See original summary.

neslee canil pinto’s picture

Status: Active » Closed (works as designed)

Yes you can do this is module configuration page - /admin/config/quick-node-clone

sissonen’s picture

Status: Closed (works as designed) » Active

Yes, you can exclude a field but a field excluded from cloning does not get the default value of the field. The field is only wiped clean of content. Just tested this on simplytest.me and Quick node clone v. 8.x-1.13.

My suggestion would be: if a field is chosen to be excluded add a checkbox with "Use default value of the field".

luisj0_c’s picture

I encountered a similar situation, and what I did was to set the default value using \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);

I hope this information can be helpful. Below are more details:

diff --git a/modules/contrib/quick_node_clone/src/Entity/QuickNodeCloneEntityFormBuilder.php b/modules/contrib/quick_node_clone/src/Entity/QuickNodeCloneEntityFormBuilder.php
index b917a553..2e013b70 100644
--- a/modules/contrib/quick_node_clone/src/Entity/QuickNodeCloneEntityFormBuilder.php
+++ b/modules/contrib/quick_node_clone/src/Entity/QuickNodeCloneEntityFormBuilder.php
@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
+use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupContent;
@@ -152,12 +153,19 @@ public function getForm(EntityInterface $original_entity, $operation = 'default'
$translated_node = $new_node->getTranslation($langcode);
$translated_node = $this->cloneParagraphs($translated_node);
$this->moduleHandler->alter('cloned_node', $translated_node, $original_entity);
+ $field_definitions = $this->get_field_definitions($translated_node->getEntityTypeId(),$translated_node->bundle());

// Unset excluded fields.
$config_name = 'exclude.node.' . $translated_node->getType();
if ($exclude_fields = $this->getConfigSettings($config_name)) {
foreach ($exclude_fields as $field) {
- unset($translated_node->{$field});
+ $default_value = $field_definitions[$field]->getDefaultValue($translated_node);
+ if(count($default_value) == 0){
+ unset($translated_node->{$field});
+ }
+ else{
+ $translated_node->set($field, $default_value);
+ }
}
}

@@ -280,4 +288,21 @@ public function getConfigSettings($value) {
return $settings;
}

+ /**
+ * Fetch the field definitions.
+ */
+ public function get_field_definitions($entity_type, $bundle, $field_definitions = NULL) {
+ if ($field_definitions === NULL) {
+ $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
+ }
+
+ $field_definitions = array_filter($field_definitions, function ($field_definition) {
+ return $field_definition instanceof FieldConfigInterface;
+ });
+
+ uasort($field_definitions, ['\Drupal\Core\Config\Entity\ConfigEntityBase', 'sort']);
+
+ return $field_definitions;
+ }
+
}