spicywebau / craft-neo

A Matrix-like field type for Craft CMS that uses existing fields
Other
403 stars 63 forks source link

Upgrading from 2.13.19 to 4.0.6 gives a sortOrder error #858

Closed larsboldt closed 4 months ago

larsboldt commented 4 months ago

Bug Description

Upgrading from CraftCMS 3.9.10 to 4.8.0 and Neo from 2.13.19 to 4.0.6 gives the following exception when applying project config:

{"Source":"Craft CMS","DateTime":"2024-02-29T13:08:15Z","Level":1,"LevelName":"ERROR","Category":"yii\\base\\ErrorException:2","Message":"Undefined array key \"sortOrder\"","Context":{"Exception":{"Message":"Undefined array key \"sortOrder\"","Code":2,"File":"/app/vendor/spicyweb/craft-neo/src/services/BlockTypes.php:544","Class":"yii\\base\\ErrorException","StackTrace":"#0 /app/vendor/craftcms/cms/src/web/ErrorHandler.php(79): yii\\base\\ErrorHandler->handleError(2, 'Undefined array...', '/app/vendor/spi...', 544)\n#1 /app/vendor/spicyweb/craft-neo/src/services/BlockTypes.php(544): craft\\web\\ErrorHandler->handleError(2, 'Undefined array...', '/app/vendor/spi...', 544)\n#2 /app/vendor/craftcms/cms/src/services/ProjectConfig.php(1143): benf\\neo\\services\\BlockTypes->handleChangedBlockType(Object(craft\\events\\ConfigEvent))\n#3 [internal function]: craft\\services\\ProjectConfig->handleChangeEvent(Object(craft\\events\\ConfigEvent))\n#4 /app/vendor/yiisoft/yii2/base/Component.php(633): call_user_func(Array, Object(craft\\events\\ConfigEvent))\n#5 /app/vendor/craftcms/cms/src/models/ProjectConfigData.php(85): yii\\base\\Component->trigger('updateItem', Object(craft\\events\\ConfigEvent))\n#6 /app/vendor/craftcms/cms/src/services/ProjectConfig.php(1255): craft\\models\\ProjectConfigData->commitChanges(Array, Array, 'neoBlockTypes.1...', false, NULL, true)\n#7 /app/vendor/craftcms/cms/src/services/ProjectConfig.php(1261): craft\\services\\ProjectConfig->craft\\services\\{closure}('neoBlockTypes.1...')\n#8 /app/vendor/craftcms/cms/src/services/ProjectConfig.php(592): craft\\services\\ProjectConfig->_applyChanges(Array, Object(craft\\models\\ProjectConfigData), Object(craft\\models\\ReadOnlyProjectConfigData))\n#9 /app/vendor/craftcms/cms/src/controllers/ConfigSyncController.php(60): craft\\services\\ProjectConfig->applyExternalChanges()\n#10 [internal function]: craft\\controllers\\ConfigSyncController->actionApplyYamlChanges()\n#11 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)\n#12 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\\base\\InlineAction->runWithParams(Array)\n#13 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\\base\\Controller->runAction('apply-yaml-chan...', Array)\n#14 /app/vendor/craftcms/cms/src/web/Application.php(341): yii\\base\\Module->runAction('config-sync/app...', Array)\n#15 /app/vendor/craftcms/cms/src/web/Application.php(642): craft\\web\\Application->runAction('config-sync/app...', Array)\n#16 /app/vendor/craftcms/cms/src/web/Application.php(303): craft\\web\\Application->_processActionRequest(Object(craft\\web\\Request))\n#17 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\\web\\Application->handleRequest(Object(craft\\web\\Request))\n#18 /app/web/index.php(13): yii\\base\\Application->run()\n#19 {main}"}}}

Steps to reproduce

  1. Upgrade versions in composer
  2. Let Craft do its db upgrade
  3. Apply project config changes

Expected behaviour

No response

Neo version

4.0.6

Craft CMS version

4.8.0

What is the affected Neo field's propagation method?

No response

Does this issue involve templating, and if so, is eager-loading used?

This is not a templating issue

ttempleton commented 4 months ago

I'm unable to reproduce this; if possible, could you please send your composer files and database backup to plugins@spicyweb.com.au and we'll have a look.

larsboldt commented 4 months ago

Files sent!

ttempleton commented 4 months ago

Thanks for sending those. It looks like some block types from the same field have somehow been assigned the same sortOrder in the database, so our migration in Neo 4 to convert the sortOrder values in the project config to ordered arrays stored at the neo.orders.<field uid> path would not include some of the affected block types. Since those block types then have neither a sortOrder value nor an item in the neo.orders.<field uid> array in the project config, this error occurs.

Unfortunately, I'm not really sure why some block types have ended up with the same sortOrder - maybe an old Neo bug that I can't remember - but I think the best way to resolve it would be to temporarily revert to your Craft 3 composer files, and resave the Neo field with the affected block types, without making any changes, which should then force them to have different sortOrder values. Then upgrade to Craft 4, and this error should be gone. (Without wanting to give away too much publicly, the last letter in the affected Neo field's name is e.)

I'll close this issue for now, but please let me know if the above does not resolve the issue.

larsboldt commented 3 months ago

Did as suggested and it seems to have worked. Thanks!