🇬🇹Guatemala @luisj0_c

Account created on 22 July 2021, over 3 years ago
#

Recent comments

🇬🇹Guatemala luisj0_c

I also have the same scenario as @delacosta456 and have confirmed that it is happening.

🇬🇹Guatemala luisj0_c

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;
+ }
+
}

Production build 0.71.5 2024