verbb / vizy

A flexible visual editor for Craft CMS
Other
43 stars 8 forks source link

Deleting certain rows in Table within a Vizy Field causes: array_key_exists() expects parameter 2 to be array, null given #200

Closed zollf closed 1 year ago

zollf commented 1 year ago

Describe the bug

Deleting any row but the last row in a Table Field within a Vizy Field and saving causes an error. It looks like null is being put in place of deleted rows, then when the Table Field tries to normalize its values, it fails because of the null.

When just using a Table Field by itself (not within a Vizy Field), it works as intended, which is that you can delete any single row without an error.

Stack trace:

TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /app/vendor/craftcms/cms/src/fields/Table.php:396
Stack trace:
#0 /app/vendor/craftcms/cms/src/fields/Table.php(396): array_key_exists('col1', NULL)
#1 /app/vendor/craftcms/cms/src/base/Element.php(4346): craft\fields\Table->normalizeValue(Array, Object(verbb\vizy\elements\Block))
#2 /app/vendor/craftcms/cms/src/base/Element.php(3401): craft\base\Element->normalizeFieldValue('table')
#3 /app/vendor/craftcms/cms/src/base/Element.php(1805): craft\base\Element->getFieldValue('table')
#4 /app/vendor/yiisoft/yii2/validators/Validator.php(258): craft\base\Element->__get('field:table')
#5 /app/vendor/yiisoft/yii2/base/Model.php(367): yii\validators\Validator->validateAttributes(Object(verbb\vizy\elements\Block), Array)
#6 /app/vendor/verbb/vizy/src/fields/VizyField.php(554): yii\base\Model->validate()
#7 /app/vendor/craftcms/cms/src/base/Element.php(2169): verbb\vizy\fields\VizyField->validateBlocks(Object(craft\elements\Entry), NULL)
#8 /app/vendor/yiisoft/yii2/validators/InlineValidator.php(84): craft\base\Element->validateCustomFieldAttribute('field:vizyWithT...', Array, Object(yii\validators\InlineValidator), Object(verbb\vizy\models\NodeCollection))
#9 /app/vendor/yiisoft/yii2/validators/Validator.php(261): yii\validators\InlineValidator->validateAttribute(Object(craft\elements\Entry), 'field:vizyWithT...')
#10 /app/vendor/yiisoft/yii2/base/Model.php(367): yii\validators\Validator->validateAttributes(Object(craft\elements\Entry), Array)
#11 /app/vendor/craftcms/cms/src/services/Elements.php(2569): yii\base\Model->validate()
#12 /app/vendor/craftcms/cms/src/services/Elements.php(785): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, NULL)
#13 /app/vendor/craftcms/cms/src/controllers/EntryRevisionsController.php(506): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#14 [internal function]: craft\controllers\EntryRevisionsController->actionPublishDraft()
#15 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#16 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#17 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('publish-draft', Array)
#18 /app/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('entry-revisions...', Array)
#19 /app/vendor/craftcms/cms/src/web/Application.php(602): craft\web\Application->runAction('entry-revisions...', Array)
#20 /app/vendor/craftcms/cms/src/web/Application.php(272): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#21 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#22 /app/web/index.php(12): yii\base\Application->run()
#23 {main}

Further context: image

You can see above that the value being passed into the table is:

[null, ['col1' => 'test2'], ['col1' => 'test3']

This is also reflected when I toggle Vizy HTML to see what the raw value looks like when I delete a row in the table.

<vizy-block>{"id":"vizy-block-pGILSUZXxJ","enabled":true,"collapsed":false,"values":{"type":"type-16FdC7kore","content":{"fields":{"table":[null,{"col1":"test2"},{"col1":"test3"}]}}}}</vizy-block>

Steps to reproduce

  1. Create a Table Field with any configuration
  2. Create a Vizy Field that includes the new Table Field as a component
  3. Add the new Vizy Field to a section so you can interact with it
  4. Within the selected section, create a new entry, add multiple rows to the table (in my case I did test1, test2, and test3), and save
  5. Delete any row but the last (so test1 and/or test2), then save.
  6. The error should now appear

Craft CMS version

3.8.5

Plugin version

1.0.22

Multi-site?

No

Additional context

No response

engram-design commented 1 year ago

That's an interesting one indeed! I'm not even sure how that's possible, but looking into it as I can replicate it on my end.

engram-design commented 1 year ago

Fixed in 2.1.0

zollf commented 1 year ago

@engram-design thanks for the update. Is it possible to cherry pick the commits relating to this issue into craft-3 support? We are still using Craft 3 with Vizy

engram-design commented 1 year ago

It's a bit more complicated than just that commit (which refers to picking Table nodes, not Craft Table fields in Vizy Block nodes). But I believe the fix can still be backported.

Fixed for the next release. To get this early, run composer require verbb/vizy:"dev-craft-3 as 1.0.22"

zollf commented 1 year ago

@engram-design thanks!