Complex UI component, using dynamic rows in admin, for a product attribute, is not saving when removing all rows

magento.stackexchange https://magento.stackexchange.com/questions/283555

Pergunta

I created a complex product attribute entry in product admin, which uses dynamic rows.

Same idea as this core one:

enter image description here

UI works fine, but if you remove all rows, and save, the attribute does not update, and reloads back to the rows prior to removed. So, basically, you cannot clear out the rows/entries.

However, this example core one works 100%.

My entry is adjusted using UI Components modifiers, exactly like this core attribute is done.

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/AdvancedPricing.php#L433

(they used a derived UI component called Magento_Catalog/js/components/dynamic-rows-tier-price in place of Magento_Catalog/js/components/dynamic-rows, but they are essentially the same. Tested as well by making my component use the same, which did not solve the issue (just in case)

So, essentially, we have ability to build these complex input components, but they cannot be cleared.

Foi útil?

Solução

I am placing this answer, to allow someone having same issue get help.

Investigating how magento core solves this issue (thinking I am missing something in my UI Component setup), I found this line of code:

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php#L202

$productData['tier_price'] = isset($productData['tier_price']) ? $productData['tier_price'] : [];

So, seems like this issue is solved (by core) as part of the product data initialisation

Essentially, the issue is that since the attribute contains no data, at saving, it is not in the POST data, so is not handled.

That line of code solves this.

The solution I did was to place a before interceptor to the initializeFromData method, and handle my own attribute the same.

Personally, I think this stinks. The UI component system should have a way to deal with this, and not need to place such code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top