【发布时间】:2012-04-25 21:58:47
【问题描述】:
我有一个实体。当实体在弹出窗口中加载时,我需要执行一些 JS 代码。 有没有办法通过管理类配置将自定义 JS/HTML 代码添加到实体表单。例如。将模板作为选项传递
【问题讨论】:
我有一个实体。当实体在弹出窗口中加载时,我需要执行一些 JS 代码。 有没有办法通过管理类配置将自定义 JS/HTML 代码添加到实体表单。例如。将模板作为选项传递
【问题讨论】:
你可以这样做:-
像这样在你的 FormMapper 中添加一个类参数:-
受保护的函数 configureFormFields(FormMapper $formMapper) { $formMapper ->add('description', null, array('attr' => array('class' => 'for_popup'), 'required' => false)) }
从 Sonata CRUD 模板扩展 edit.html.twig / base_edit.html.twig
---edit.html.twig----
{% extends 'YourBundle:YourAdminClass:base_edit.html.twig' %}
---base_edit.html.twig---
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
// Your JS code here
</script>
{% endblock %}
使用您的edit.html.twig 代替 Sonata CRUD,方法是在 getEditTemplate 函数中定义它(在您的 Admin 类中)。
public function getEditTemplate()
{
return 'YourAdminBundle:ControllerName:edit.html.twig';
}
您还可以在注入管理服务时设置自定义编辑模板。
<service id="sonata.admin.bf" class="Wyzbiz\Bundle\MainBundle\Admin\BfAdmin">
<tag name="sonata.admin" manager_type="orm" group="Content" label="BFs"/>
<argument />
<argument>Wyzbiz\Bundle\MainBundle\Entity\Bf</argument>
<argument>WyzbizMainBundle:CRUD</argument>
<call method="setTranslationDomain"><argument>WyzbizMainBundle</argument></call>
<call method="setTemplate"><argument>list</argument>
<argument>WyzbizMainBundle:CRUD/Bf:list.html.twig</argument></call>
</service>
【讨论】:
@Jessica 您可以添加自己的 getTemplate 方法实现,而不是在管理类的 configureFormFields 方法中使用 $this->setTemplate(),我的方法如下所示:
/**
* Override core method to display custom template(s)
*/
public function getTemplate($name)
{
switch ($name) {
case 'edit':
return 'YourAdminBundle:YourAdminEntity:edit.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
【讨论】: