如何在 SonataAdmin 中自定义特定的字段渲染?

How to customize particular field rendering in SonataAdmin?

在我的 Admin class 中,在 configureFormFields 方法中我有复选框字段 excerptImageSide:

protected function configureFormFields(FormMapper $formMapper)
{
     $formMapper
      // ...
         ->add('excerptImageSide')
     ;
} 

现在在管理中 create/edit 页面复选框呈现为:

如您所见,复选框位于标签下方,但我希望将其放在单行中。所以我的问题是如何为特定的字段渲染应用自定义模板?

我知道的更简单的方法是将字段包装在具有 class 的 ->with(...) 中,并进行一些自定义 CSS:

管理员

 $formMapper
  // ...
     ->with('YourSection', array('class' => 'floating-checkboxes'))
         ->add('excerptImageSide')
     ->end()
 ;

CSS

.floating-checkboxes label{
    float: left;      // Make the label floating
    min-width: 185px; // Keep a correct space between the field and its label
}

要加载您的自定义样式表,或完全覆盖字段模板,请查看 advanced configuration documentation chapter