Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: Fields with enum as value type fail while saving #12297

Closed
gglnx opened this issue Nov 11, 2022 · 5 comments · Fixed by #12304
Closed

[4.x]: Fields with enum as value type fail while saving #12297

gglnx opened this issue Nov 11, 2022 · 5 comments · Fixed by #12304
Assignees

Comments

@gglnx
Copy link
Contributor

gglnx commented Nov 11, 2022

What happened?

Description

I've build an field type which uses an enum as value type. While saving craft\services\Elements::duplicateElement() at line 1461 tries to clone the enum, but cloning of enums is not supported by PHP. A simple check with instanceof \UnitEnum (https://www.php.net/manual/en/class.unitenum.php) should fix that.

Steps to reproduce

  1. Build a field type that uses enum as value type, for example: https://gist.github.com/gglnx/33b74794af4462f51a55532c71bb3071
  2. Try to save an element with this field type

Craft CMS version

4.3.1

PHP version

8.1.11

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@brianjhanson brianjhanson self-assigned this Nov 11, 2022
brianjhanson added a commit that referenced this issue Nov 11, 2022
brianjhanson added a commit that referenced this issue Nov 11, 2022
@brianjhanson
Copy link
Contributor

Thanks for reporting and for the sample code! It was very helpful. I've opened a PR for this. We can't fully support enums until we require 8.1 but it should help out in the mean time.

@brandonkelly
Copy link
Member

Craft 4.3.2 is out with that fix.

@gglnx
Copy link
Contributor Author

gglnx commented Mar 16, 2023

@brandonkelly looks like your change in a4303f1 this broke my enum fields :(

Error: Trying to clone an uncloneable object of class Enum\EntryOrderEnum in vendor/craftcms/cms/src/services/Elements.php:1491
Stack trace:
#0 vendor/craftcms/cms/src/services/Revisions.php(162): craft\services\Elements->duplicateElement(Object(craft\elements\MatrixBlock), Array)
#1 vendor/craftcms/cms/src/services/Matrix.php(1035): craft\services\Revisions->createRevision(Object(craft\elements\MatrixBlock), 1, NULL, Array)
#2 vendor/craftcms/cms/src/fields/Matrix.php(1059): craft\services\Matrix->createRevisionBlocks(Object(craft\fields\Matrix), Object(craft\elements\Entry), Object(craft\elements\Entry))
#3 vendor/craftcms/cms/src/base/Element.php(4947): craft\fields\Matrix->afterElementPropagate(Object(craft\elements\Entry), true)
#4 vendor/craftcms/cms/src/elements/Entry.php(1976): craft\base\Element->afterPropagate(true)
#5 vendor/craftcms/cms/src/services/Elements.php(1641): craft\elements\Entry->afterPropagate(true)
#6 vendor/craftcms/cms/src/services/Revisions.php(162): craft\services\Elements->duplicateElement(Object(craft\elements\Entry), Array)
#7 vendor/craftcms/cms/src/elements/Entry.php(1980): craft\services\Revisions->createRevision(Object(craft\elements\Entry), 1, '')
#8 vendor/craftcms/cms/src/services/Elements.php(3164): craft\elements\Entry->afterPropagate(false)
#9 vendor/craftcms/cms/src/services/Elements.php(1063): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true, Array, false)
#10 vendor/craftcms/cms/src/controllers/ElementsController.php(957): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#11 [internal function]: craft\controllers\ElementsController->actionSave()
#12 vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#13 vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#14 vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('save', Array)
#15 vendor/craftcms/cms/src/web/Application.php(302): yii\base\Module->runAction('elements/save', Array)
#16 vendor/craftcms/cms/src/web/Application.php(627): craft\web\Application->runAction('elements/save', Array)
#17 vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#18 vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#19 web/index.php(14): yii\base\Application->run()
#20 {main}

@gglnx
Copy link
Contributor Author

gglnx commented Mar 16, 2023

8.1+ reports for class_exists(UnitEnum::class) false too: https://3v4l.org/oPA8A

brandonkelly added a commit that referenced this issue Mar 16, 2023
@brandonkelly
Copy link
Member

@gglnx Doh, UnitEnum is an interface, not a class. Just released 4.4.3 with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants