-
Notifications
You must be signed in to change notification settings - Fork 915
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
[Feature] Conditionable trait #5035
Conversation
[ci skip] [skip ci]
[ci skip] [skip ci]
Sounds good to me!
Hmm I don't understand why this would be useful to be honest... a // this
$this->crud->when($condition, function (CrudPanel $crud) {
$crud->field('email')
->label('Email Address')
->type('email')
// Unless $anotherCondition is met, let's add a hint!
->unless($anotherCondition, function (CrudField $field) {
$field->hint('My hint')
// When the operation is create, let's move the field after name
->when($this->crud->getOperation() === 'create', function (CrudField $field) {
$field->after('name');
});
})
// Finally, if the operation is update, let's put this field into a tab!
->when($this->crud->getOperation() === 'update', function (CrudField $field) {
$field->tab('My tab');
});
});
// is the same as doing
if ($condition) {
$crud->field('email')
->label('Email Address')
->type('email')
// Unless $anotherCondition is met, let's add a hint!
->unless($anotherCondition, function (CrudField $field) {
$field->hint('My hint')
// When the operation is create, let's move the field after name
->when($this->crud->getOperation() === 'create', function (CrudField $field) {
$field->after('name');
});
})
// Finally, if the operation is update, let's put this field into a tab!
->when($this->crud->getOperation() === 'update', function (CrudField $field) {
$field->tab('My tab');
});
} Or maybe the example wasn't the best, and there's a real use case for |
When this is ready, please
|
Yeah, probably not needed. I thought it was going to clean up a bit the code.... but it turns out we save only one line (although it still looks cleaner to me). What do you think @pxpm ? Can there be any use of adding |
Hey guys. I think "at the present moment", there is utility for it in the I am not sure if we want to encourage people to use those methods, as the plan is to move those methods out of CrudPanel in the future. We want to encourage the usage of our class objects like I don't agree with @tabacitu when he says "it's only one less line". Basically it's only less one line for pretty much everything, it's just a conditional after all. In your example I agree with @maurohmartinez that it looks better without the My only concern is adding it at this point to the CrudField::name('email')
->label('Email Address')
->type('email')
// Unless $anotherCondition is met, let's add a hint!
->unless($anotherCondition, function (CrudField $field) {
$field->hint('My hint')
// When the operation is create, let's move the field after name
->when($this->crud->getOperation() === 'create', function (CrudField $field) {
$field->after('name');
});
})
// Finally, if the operation is update, let's put this field into a tab!
->when($this->crud->getOperation() === 'update', function (CrudField $field) {
$field->tab('My tab');
}); But still, what if you want this field only if user is admin ? Maybe we can overwrite the // only add this field when the user is admin
CrudField::when(backpack_user()->hasRole('admin'))
->name('email')
->label('Email Address')
->type('email'); See ? one less if, no mention of |
@pxpm I must say I love the syntax of your last example! My question is, in case the condition is not met, how do we handle it? If we return |
Ouch! Good catch Pedro. Ok it's settled, no conditionable on
🎰 Ding ding ding ding! It might not be possible. But... and I will hate myself for saying this... it also might be possible 😅 - Laravel has the At this moment... in v5... we either implement |
Hmm as the original PR creator, here is my 2 cents
function rules(array|callable|null $abc): ?array {
// 1-2 famous laravel libraries used this to great effect
} But in v5 where the minimum PHP is 7.3, when() & unless() is very useful. (Could be in v6 too for things that don't have a dedicated method yet.
return [
// if (abc) { here? oh no! fluent methods would be awesome here!
] As a side note, in my 1 backpack admin site I still define my backpack fields the original way (the non fluent way) because I'm used to it (the original non fluent way). So it's a bit funny I made this PR when I'm generally not a practitioner of the fluent way in backpack. 🤣
But again, this is a tricky topic & an art, so if you are divided about it, can choose not to add to CrudPanel 1st and decide again later. |
I love it too, if we can think of a way to make it work ... it's sugar! Instead of making it a constructor, we can make a Facade for It's exactly the premise of Let's leave this for a future talk or @tabacitu is going to have a stroke now, I will give my shoot at this because sometimes I got lucky 🍀 🙃 👍 @tabacitu the way I see it, at this moment what we are adding is: a way for developers to conditionally set the attributes on crud objects, we are not really making the class (CrudField) itself conditionable. I think the point of Conditionable is to make full classes conditionable, see laravel/framework#46259 for example about the CrudField::when($isAdmin, function($field) {
$field->name('my_admin_field')->unless(...)
})
// V.S
if($isAdmin) {
CrudField::name('my_admin_field')->unless(...)
} We are adding only the support for the second example, seems weird having I would still not add this to CrudPanel, at least for now. Once added can't be removed, and we plan to mess with that class in the future. Nobody ever asked for this, so let's keep it simple and contained. Cheers |
☝️ |
Ready from my point of view! But I'll let @pxpm double-check, merge and release this - 5.x is in his court. Feel free to ping him if he doesn't reply soon 😅 We can call it 5.6.0 I think - it's a feature... 🤷♂️ |
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.
Sounds awesome! Two features, one release 💪🎉 |
WHY
Everything about it here.
Credits to @ziming
PR for docs here.
I propose to add it to:
CrudColumn
CrudButton
CrudField
already done on this PRCrudFilter
CrudPanel
to do things like$this->crud->when($condition, function (CrudPanel $crud) {...}
. I think it's useful since very often we set values for settings conditionally.Sample of a crud field to use in docs:
How about documenting this here?