craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[5.x]: Errors when editing matrix blocks in product and variant fields #15362

Closed bavodeclercq closed 3 months ago

bavodeclercq commented 4 months ago

What happened?

Description

I have a matrix field used as a content builder with the view mode set to cards. When I add this matrix field to the product fields and/or variant fields of a product type, I can add blocks without any issues. However, when I attempt to edit a block, I encounter the following errors:

Product Fields:

Calling unknown method: craft\commerce\elements\Product::getOwner()

Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2350): yii\base\Component->__call()
craftcms/commerce#1 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3869): craft\base\Element->__call()
craftcms/commerce#2 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3874): craft\services\Elements->updateSearchIndex()
craftcms/commerce#3 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3787): craft\services\Elements->updateSearchIndex()
craftcms/commerce#4 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1170): craft\services\Elements->craft\services\{closure}()
craftcms/commerce#5 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3498): craft\services\Elements->ensureBulkOp()
craftcms/commerce#6 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1825): craft\services\Elements->_saveElementInternal()
craftcms/commerce#7 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1170): craft\services\Elements->craft\services\{closure}()
craftcms/commerce#8 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1812): craft\services\Elements->ensureBulkOp()
craftcms/commerce#9 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1441): craft\services\Elements->duplicateElement()
craftcms/commerce#10 /var/www/html/vendor/craftcms/cms/src/services/Drafts.php(322): craft\services\Elements->updateCanonicalElement()
craftcms/commerce#11 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(1713): craft\services\Drafts->applyDraft()
craftcms/commerce#12 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
craftcms/commerce#13 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()
craftcms/commerce#14 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams()
craftcms/commerce#15 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction()
craftcms/commerce#16 /var/www/html/vendor/craftcms/cms/src/web/Application.php(349): yii\base\Module->runAction()
craftcms/commerce#17 /var/www/html/vendor/craftcms/cms/src/web/Application.php(650): craft\web\Application->runAction()
craftcms/commerce#18 /var/www/html/vendor/craftcms/cms/src/web/Application.php(311): craft\web\Application->_processActionRequest()
craftcms/commerce#19 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest()
craftcms/commerce#20 /var/www/html/web/index.php(12): yii\base\Application->run()
craftcms/commerce#21 {main} {"memory":17370864,"exception":"[object] (yii\\base\\UnknownMethodException(code: 0): Calling unknown method: craft\\commerce\\elements\\Product::getOwner() at /var/www/html/vendor/yiisoft/yii2/base/Component.php:308)"} 

Variant Fields:

craft\models\FieldLayout::getFieldByld(): Argument craftcms/commerce#1 ($id) must be of type int, null given, called in /var/www/html/vendor/craftcms/cms/src/services/Elements.php on line 3872

Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3872): craft\models\FieldLayout->getFieldById()
craftcms/commerce#1 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3874): craft\services\Elements->updateSearchIndex()
craftcms/commerce#2 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3787): craft\services\Elements->updateSearchIndex()
craftcms/commerce#3 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1170): craft\services\Elements->craft\services\{closure}()
craftcms/commerce#4 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(3498): craft\services\Elements->ensureBulkOp()
craftcms/commerce#5 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1825): craft\services\Elements->_saveElementInternal()
craftcms/commerce#6 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1170): craft\services\Elements->craft\services\{closure}()
craftcms/commerce#7 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1812): craft\services\Elements->ensureBulkOp()
craftcms/commerce#8 /var/www/html/vendor/craftcms/cms/src/services/Elements.php(1441): craft\services\Elements->duplicateElement()
craftcms/commerce#9 /var/www/html/vendor/craftcms/cms/src/services/Drafts.php(322): craft\services\Elements->updateCanonicalElement()
craftcms/commerce#10 /var/www/html/vendor/craftcms/cms/src/controllers/ElementsController.php(1713): craft\services\Drafts->applyDraft()
craftcms/commerce#11 [internal function]: craft\controllers\ElementsController->actionApplyDraft()
craftcms/commerce#12 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()
craftcms/commerce#13 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams()
craftcms/commerce#14 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction()
craftcms/commerce#15 /var/www/html/vendor/craftcms/cms/src/web/Application.php(349): yii\base\Module->runAction()
craftcms/commerce#16 /var/www/html/vendor/craftcms/cms/src/web/Application.php(650): craft\web\Application->runAction()
craftcms/commerce#17 /var/www/html/vendor/craftcms/cms/src/web/Application.php(311): craft\web\Application->_processActionRequest()
craftcms/commerce#18 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest()
craftcms/commerce#19 /var/www/html/web/index.php(12): yii\base\Application->run()
craftcms/commerce#20 {main} {"memory":17809672,"exception":"[object] (TypeError(code: 0): craft\\models\\FieldLayout::getFieldById(): Argument craftcms/commerce#1 ($id) must be of type int, null given, called in /var/www/html/vendor/craftcms/cms/src/services/Elements.php on line 3872 at /var/www/html/vendor/craftcms/cms/src/models/FieldLayout.php:731)"} 

Additionally, if I change the view mode of the matrix field to inline-editable blocks, I can save without errors, but the changes are not stored. (This may have worked correctly the first time, but after switching back to card view, the blocks appeared as edited, and I discarded the changes. Since then, changes are not saved. I don't recall if it worked correctly before, but I think it did.)

Steps to reproduce

  1. Create a matrix field with the view mode set to cards.
  2. Add this matrix field to the product fields and/or variant fields of a product type.
  3. Add blocks to the matrix field.
  4. Attempt to edit a block in the matrix field.
  5. Observe the errors mentioned above.

Expected behavior

Editing a block in the matrix field should not result in errors, and changes should be saved correctly.

Actual behavior

Editing a block in the matrix field results in errors for both product and variant fields, and changes are not saved when switching to inline-editable view mode.

Craft CMS version

5.2.5

Craft Commerce version

5.0.11.1

PHP version

8.3.8

Operating system and version

Linux 6.9.6-orbstack-00147-gb0567c7c0069

Database type and version

MySQL 8.0.36

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

linear[bot] commented 4 months ago

PT-1935 [5.x]: Errors when editing matrix blocks in product and variant fields

nfourtythree commented 3 months ago

Hi @bavodeclercq

Could you try updating Commerce to the latest version (5.0.12.2) and let me know if you still see these issues?

There were also a couple of fixes in Craft itself that I think might be related to what you are seeing. These fixes were included in the 5.2.6 release of Craft.

If you could update Craft as well and see if the issues persist?

Thanks!

bavodeclercq commented 3 months ago

Hi @nfourtythree

I have updated Commerce to the latest version (5.0.12.2) and Craft to version 5.2.6, but the issue still persists on both product and variant levels.

brandonkelly commented 3 months ago

Thanks for reporting this, @bavodeclercq. It should be fixed now for the next Craft release.

To test, change your craftcms/cms requirement in composer.json to:

"craftcms/cms": "5.x-dev as 5.2.6",

Then run composer update.

bavodeclercq commented 3 months ago

Hi @brandonkelly

Thank you for the fix. I tested it and the issue is resolved at the product level. However, the problem still persists at the variant level.

Variant level error

brandonkelly commented 3 months ago

Hey @bavodeclercq, can you run composer update again?

brandonkelly commented 3 months ago

Craft 5.2.7 is out with the fix for this. Thanks for reporting!

bavodeclercq commented 3 months ago

After the latest update, the issue is now also fixed for variants. Thanks!