问题描述
我试图覆盖 SonataUser/Admin/Model/UserAdmin
的 configureFormFields()
因为我需要从管理表单中删除一些默认字段.所以我复制了文件 vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php
在我的包 app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php
并修改它.然后将其声明为服务:
I'm trying to override the SonataUser/Admin/Model/UserAdmin
's configureFormFields()
because I need to remove some default fields from the admin form.So I have copied the file vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php
in my bundle app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php
and modified it. Then declared it as a service:
# app/application/Sonata/UserBundle/Resources/config/services.yml
services:
application_user.registration.form.type:
class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
arguments: [%sonata_user.model.user.class%]
tags:
- { name: form.type, alias: application_user_admin }
现在问题:我做得对吗?我如何告诉奏鸣曲管理员使用它?
Now questions:Am I doing right ? How can I tell sonata admin to use it ?
推荐答案
覆盖类应该在 config.yml 中设置:
The overriding class should be set in config.yml:
# app/config/config.yml
sonata_user:
admin:
user:
class: MyCompany\UserBundle\Admin\Model\UserAdmin
扩展原来的 UserAdmin:
Extend original UserAdmin:
namespace MyCompany\UserBundle\Admin\Model;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
// new logic
}
}
当然要更改类名 MyCompany\UserBundle\Admin\Model\UserAdmin
以反映您的包结构.
Of course change class name MyCompany\UserBundle\Admin\Model\UserAdmin
to reflect your bundle structure.
这篇关于覆盖用户管理表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!