-
Notifications
You must be signed in to change notification settings - Fork 916
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
PHP Enums support #4678
PHP Enums support #4678
Conversation
[ci skip] [skip ci]
Clicking Edit to show the edit form get me |
Should be good now @ziming I will write some tests for it 👍 |
Looks good so far. but I have only tested with backed enums. From the code it does look like it will work for Non-backed Enum too |
[ci skip] [skip ci]
… into enum-support # Conflicts: # tests/Unit/CrudPanel/CrudPanelUpdateTest.php # tests81/Unit/Models/ArticleWithEnum.php # tests81/Unit/Models/Enums/StatusEnum.php
[ci skip] [skip ci]
[ci skip] [skip ci]
I assigned @jorgetwgroup for testing purposes, and @tabacitu for implementation decisions. We can also create a |
|
||
public static function getOptions() | ||
{ | ||
return array_combine(array_column(self::cases(), 'name'), array_column(self::cases(), 'value')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return array_combine(array_column(self::cases(), 'name'), array_column(self::cases(), 'value')); | |
return array_column(self::cases(), 'value', 'name'); |
The resulting array will be
[
'NAME_1' => 'Name 1 Value',
'NAME_2' => 'Name 2 Value',
]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pedro an I have decided to add enum support a different way - by creating enum
field and column (actually adding functionality to the enum field but you know what I mean).
That should make this change super-non-breaking, with zero touches to the core 💪
there is already a database enum field in backpack so are you all going to call it php enum to differentiate it? edit: ah i see so now the enum field support both database and php enum |
No zimming, the enum field should work for both enum types (PHP and database) I already did that change in this PR, I am finishing now the enum column too. |
@ziming I think it's now finished, tests are passing and I added the column too. If you give this a test and found something let me know. I am now going to write the docs for it. Cheers |
Added docs for field and column Laravel-Backpack/docs#379 |
Looking forward to this PR. Hopefully it gets back ported to laravel 8 too. Which sometimes happen |
Hello @pxpm thank to you because in this revision everything works as we speak about it. I will go step by step. Add field "using the same function as column" On my side everything is working as expected. About code for me look everything ok, just a warning for @tabacitu this required PHP 8.1 to work normal. Cheers. |
WHY
BEFORE - What was wrong? What was happening before this PR?
A powerfull introduction in 8.1 was Enums. Backpack was completely un-ware of them until now.
It was not possible use a field casted to an
Enum::class
.See the following example, a little adaptation of the
Article
model andArticleCrudController
:AFTER - What is happening after this PR?
After this PR it works as expected.
HOW
How did you achieve that, in technical terms?
Basically added a layer between getting and returning the models attributes values.
That layer will check if Enums is a thing (only available in 8.1), if they are check if the model attribute is any kind of Enum, in case it is, return the appropriate string representation of it.
NOTE For the List/ShowOperation i started from #4633 and added support for text and select_from_array columns (probably the ones that make sense to use with an Enum ? )
Is it a breaking change?
I don't think so, no.
How can we test the before & after?
You can use the example I gave on this issue description.