-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Allow to override any action easily #283
Conversation
For the |
Might I also suggest using a system like the one you have in the twig extensions, like this: // Entity currently used: Post
$this->resolveAction('new'); // Returns 'newPostAction'
$this->resolveAction('prePersist*Entity'); // Returns 'prePersistPostEntity' When there is no wildcard, the entity name is appended. Unless, the wildcard is replaced by the entity name. |
$fields = $fields = $this->entity['new']['fields']; | ||
$newForm = $this->createNewForm($item, $fields); | ||
$fields = $this->entity['new']['fields']; | ||
$newForm = $this->createNewForm($entity, $fields); |
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.
I think we might need to check entity-specific method for the createNewForm
too
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.
createEditForm
too ?
Excluding what I said, seems all good 👍 |
I'm definitively not fan of "magic" methods like: public function createNew<EntityName>Entity()
{
...
} I don't see any value to these, as the user is already able to do that, with it's own logic and methods. The event system looks great ! 👍 |
The goal is mainly to prevent this: public function newAction() {
if ($this->entity['name'] === 'Post') {
return $this->newPostAction();
} if ($this->entity['name'] === 'Product') {
return $this->newProductAction();
} if ($this->entity['name'] === 'Category') {
return $this->newCategoryAction();
} if ($this->entity['name'] === 'Customer') {
return $this->newCustomerAction();
} if ($this->entity['name'] === 'Page') {
return $this->newPageAction();
} if ($this->entity['name'] === 'Video') {
return $this->newVideoAction();
} if ($this->entity['name'] === 'BlogArticle') {
return $this->newBlogArticleAction();
}
return parent::newAction();
} |
@Pierstoval : I understood the purpose, but I don't think this is required, and why the bundle should take care of such things. This isn't even about DX IMO. 😕 If they really need it, developers may easily create their own solutions in order to avoid such I do not know if I am making myself clear on this. But I think it's only source of trouble for easyadmin. |
@ogizanagi in my opinion this system doesn't bring magic but conventions. And I think those are two very different things. In Symfony 1 for example the filter system changed your response without you being aware of it (that's magic). In Symfony 2 if you create a file with a very specic name (e.g. Regarding the In short, I think this new feature is consisent with the rest of the bundle behavior:
|
@javiereguiluz : But the entity's name is (magic). Because the developper doesn't really have full control over it, and the name might change if there is another entity with the same name (edge case, right, but still :/ ). What do you think about what we discussed later in https://github.com/javiereguiluz/EasyAdminBundle/pull/276#issuecomment-99604657 ? I didn't realized it before with templates, but it's indeed the same problem. 😕 |
@ogizanagi yes, the entity name is "magic" for one use case: when you have totally different entities that share the same name. How often does this happen? In my experience very infrequently ... but I'd love to hear other experiences. And yes, I plan to make the entity name configurable in the future to avoid problems in this edge case. |
I think I have a solution for this problem:
easy_admin:
entities:
- 'AppBundle\Entity\Product'
- '...' If there are more than one entity with the same name, EasyAdmin autogenerates incremental names ( This is not a problem because if you are using this config format, you are obviously doing some quick-and-dirty test of this bundle and you are not going to extend the backend with a custom controller.
# config format 2
easy_admin:
entities:
Product: 'AppBundle\Entity\Product'
ProductArchive: 'AppBundle\Entity\Archive\Product'
# config format 3
easy_admin:
entities:
Product:
class: 'AppBundle\Entity\Product'
ProductArchive:
class: 'AppBundle\Entity\Archive\Product' There is only one catch: right now we use the YAML key as the label (unless you define the # config format 3
easy_admin:
entities:
'Current Products':
class: 'AppBundle\Entity\Product'
'Product Archive (2015)':
class: 'AppBundle\Entity\Archive\Product' And that would be a problem because you cannot name a PHP method as So this is my proposal:
What do you think? |
I think it's the most straightforward and ideal solution. 👍 |
@Pierstoval I like your comments about reducing code repetition and extracting the logic about checking if a custom method exists. But I'm not sure about the proposed alternatives. That's why I'm going to merge this PR as is and I'll think about how to improve that part of the code in the future. After merging this PR, this is the roadmap (to be compelted in different PRs):
|
This PR introduces a simple overriding mechanism for actions and controller methods. Examples:
Customize the instantiation of a single entity
Create a new AdminController extending from the default one and add this method:
Customize the instantiation of all entities
Create a new AdminController extending from the default one and add this method:
Tweak a specific entity before persisting/updating/removing it
Create a new AdminController extending from the default one and add this method:
Tweak all entities before persisting/updating/removing them
Create a new AdminController extending from the default one and add this method:
Tweak the list/new/edit/show/delete method for a specific entity
Create a new AdminController extending from the default one and add this method:
Tweak the list/new/edit/show/delete method for all entities
Create a new AdminController extending from the default one and add this method:
Modify the backend using events
Hook into any of the new events defined by the bundle:
Single entity actions (edit, new, delete, etc.) receive the
$entity
as the event subject. The actions related to multiple entities (list, search) receive the$paginator
object as the subject of the event. In addition, the event receives all the controller variables as arguments. You can access them viagetArgument()
or via the array access provided by Symfony for the GenericEvent: