我正在使用Sonata admin Bundle和Sonata的其他Bundle。
我正在尝试显示一些消息。它工作正常,但我不想在使用SONATA_TYPE_COLLECTION类型时显示所有字段。
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('messages', 'sonata_type_collection', array(
'label' => 'Liste des messages',
'required' => false,
'type_options' => array(
// Prevents the "Delete" option from being displayed
'delete' => true,
// 'idPackage' => false,
// 'delete_options' => array(
// // You may otherwise choose to put the field but hide it
// 'type' => 'hidden',
// // In that case, you need to fill in the options as well
// 'type_options' => array(
// 'mapped' => false,
// 'required' => false,
// )
// )
)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
))
我只想显示:
- 删除
- id
- id_user
- créerle
- 消息但没有所见即所得
是否有人有同样的问题。谢谢你的建议。
答案 0 :(得分:1)
您可以使用子管理员中的getRoot
,例如:
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
if ($this->getRoot()->getClass() == 'AppBundle\Entity\MyAdmin') {
$formMapper
->add('somefield') //this field will be only visible in the child admin form, the parent using sonata_type_collection will have a different class
;
}
}
答案 1 :(得分:0)
我为解决这个问题所做的是将link-parameter传递给add按钮:
$formMapper->add('messages', 'sonata_type_collection',
array('label' => 'whatever'),
array(
'edit' => 'inline',
'inline' => 'table',
'link_parameters' => array('owner-id' => $this->getSubject()->getId()))
);
因此,从子管理员,您可以从请求中检索这些参数,并根据需要为每个案例创建管理员:
if ($ownerId = $this->getRequest()->query->get('owner-id')) {
//create your admin differently
}
如果要更改的是已添加的字段列表(不是新字段),您可以使用:
$parent = $this->getParentFieldDescription();
if ($parent && ( ($entity = $parent->getAdmin()->getSubject()) instanceof MyInterface ) ) {
//do sth here
}
答案 2 :(得分:0)
我经常使用这种结构来查看不同位置的差异字段:
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
// Find out which admin we should render for
$admin = $this->getRequestParameter('code');
if (empty($admin)) {
$admin = $this->getRequestParameter('_sonata_admin');
}
// Show different fields based on the action you're in
switch ($admin) {
case 'app.admin.location': // this is the name in your yml file
$this->configureFormFieldsForLocationAdmin($formMapper);
break;
default:
$this->configureFormFieldsDefault($formMapper);
}
}