Active
Project:
Quick Node Clone
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
8 Feb 2019 at 11:27 UTC
Updated:
2 Oct 2020 at 07:12 UTC
Jump to comment: Most recent
Comments
Comment #2
neslee canil pintoYes you can do this is module configuration page - /admin/config/quick-node-clone
Comment #3
sissonen commentedYes, 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".
Comment #4
luisj0_c commentedI 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;
+ }
+
}