Skip to content
Snippets Groups Projects
Commit 731948a9 authored by Ben McClure's avatar Ben McClure Committed by Bojan Živanović
Browse files

Issue #2845793 by bmcclure, bojanz, ransomweaver: [Data loss] Entity trait /...

Issue #2845793 by bmcclure, bojanz, ransomweaver: [Data loss] Entity trait / product attribute / product variation type get de-selected automatically
parent 981f1985
No related branches found
Tags 8.x-2.0-beta6
No related merge requests found
......@@ -86,6 +86,7 @@ class ProductAttributeForm extends BundleEntityFormBase {
$attribute_field_map = $this->attributeFieldManager->getFieldMap();
$variation_type_storage = $this->entityTypeManager->getStorage('commerce_product_variation_type');
$variation_types = $variation_type_storage->loadMultiple();
$disabled_variation_types = [];
foreach ($variation_types as $variation_type) {
$variation_type_id = $variation_type->id();
$form['variation_types']['#options'][$variation_type_id] = $variation_type->label();
......@@ -95,12 +96,19 @@ class ProductAttributeForm extends BundleEntityFormBase {
if (in_array($attribute->id(), $used_attributes)) {
$form['original_variation_types']['#value'][$variation_type_id] = $variation_type_id;
$form['variation_types']['#default_value'][$variation_type_id] = $variation_type_id;
$form['variation_types'][$variation_type_id] = [
'#disabled' => !$this->attributeFieldManager->canDeleteField($attribute, $variation_type_id),
];
if (!$this->attributeFieldManager->canDeleteField($attribute, $variation_type_id)) {
$form['variation_types'][$variation_type_id] = [
'#disabled' => TRUE,
];
$disabled_variation_types[] = $variation_type_id;
}
}
}
}
$form['disabled_variation_types'] = [
'#type' => 'value',
'#value' => $disabled_variation_types,
];
if ($this->moduleHandler->moduleExists('content_translation')) {
$enabled = TRUE;
......@@ -371,6 +379,8 @@ class ProductAttributeForm extends BundleEntityFormBase {
$original_variation_types = $form_state->getValue('original_variation_types', []);
$variation_types = array_filter($form_state->getValue('variation_types', []));
$disabled_variation_types = $form_state->getValue('disabled_variation_types', []);
$variation_types = array_unique(array_merge($disabled_variation_types, $variation_types));
$selected_variation_types = array_diff($variation_types, $original_variation_types);
$unselected_variation_types = array_diff($original_variation_types, $variation_types);
if ($selected_variation_types) {
......
......@@ -118,13 +118,19 @@ class ProductVariationTypeForm extends CommerceBundleEntityFormBase {
'#access' => !empty($attribute_options),
];
// Disable options which cannot be unset because of existing data.
$disabled_attributes = [];
foreach ($used_attributes as $attribute_id) {
if (!$this->attributeFieldManager->canDeleteField($attributes[$attribute_id], $variation_type->id())) {
$form['attributes'][$attribute_id] = [
'#disabled' => TRUE,
];
$disabled_attributes[] = $attribute_id;
}
}
$form['disabled_attributes'] = [
'#type' => 'value',
'#value' => $disabled_attributes,
];
if ($this->moduleHandler->moduleExists('language')) {
$form['language'] = [
......@@ -165,6 +171,8 @@ class ProductVariationTypeForm extends CommerceBundleEntityFormBase {
$attribute_storage = $this->entityTypeManager->getStorage('commerce_product_attribute');
$original_attributes = $form_state->getValue('original_attributes');
$attributes = array_filter($form_state->getValue('attributes'));
$disabled_attributes = $form_state->getValue('disabled_attributes');
$attributes = array_unique(array_merge($disabled_attributes, $attributes));
$selected_attributes = array_diff($attributes, $original_attributes);
$unselected_attributes = array_diff($original_attributes, $attributes);
if ($selected_attributes) {
......
......@@ -70,6 +70,7 @@ class CommerceBundleEntityFormBase extends BundleEntityFormBase {
'#access' => count($traits) > 0,
];
// Disable options which cannot be unset because of existing data.
$disabled_traits = [];
if (!$entity->isNew()) {
foreach ($used_traits as $trait_id) {
$trait = $this->traitManager->createInstance($trait_id);
......@@ -77,9 +78,14 @@ class CommerceBundleEntityFormBase extends BundleEntityFormBase {
$form['traits'][$trait_id] = [
'#disabled' => TRUE,
];
$disabled_traits[] = $trait_id;
}
}
}
$form['disabled_traits'] = [
'#type' => 'value',
'#value' => $disabled_traits,
];
return $form;
}
......@@ -94,6 +100,8 @@ class CommerceBundleEntityFormBase extends BundleEntityFormBase {
*/
protected function validateTraitForm(array &$form, FormStateInterface $form_state) {
$traits = array_filter($form_state->getValue('traits'));
$disabled_traits = $form_state->getValue('disabled_traits');
$traits = array_unique(array_merge($disabled_traits, $traits));
$original_traits = $form_state->getValue('original_traits');
$installed_traits = [];
foreach ($original_traits as $trait_id) {
......@@ -122,10 +130,12 @@ class CommerceBundleEntityFormBase extends BundleEntityFormBase {
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
$disabled_traits = $form_state->getValue('disabled_traits');
/** @var \Drupal\commerce\Entity\CommerceBundleEntityInterface $entity */
$traits = $entity->getTraits();
$traits = array_filter($traits);
$traits = array_values($traits);
$traits = array_unique(array_merge($disabled_traits, $traits));
$entity->setTraits($traits);
return $entity;
......@@ -143,7 +153,6 @@ class CommerceBundleEntityFormBase extends BundleEntityFormBase {
$target_entity_type_id = $this->entity->getEntityType()->getBundleOf();
/** @var \Drupal\commerce\Entity\CommerceBundleEntityInterface $entity */
$entity = $this->entity;
$traits = $entity->getTraits();
$original_traits = $form_state->getValue('original_traits');
$selected_traits = array_diff($traits, $original_traits);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment