如何在 Sonata configureListFields 中检索主题?
How to retrieve subject in Sonata configureListFields?
我在我的 Symfony 项目中使用 Sonata Admin Bundle 并为我的 Article 实体创建了一个 ArticleAdmin class。
在列表页面,我添加了一些自定义操作来快速发布、取消发布、删除和预览每篇文章。
我想做的是在文章已发布时隐藏发布按钮,反之亦然。
为此,我需要访问方法 configureListFields() 中的每个对象。我会做这样的事情:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('title');
// ...
/** @var Article article */
$article = $this->getSubject();
// Actions for all items.
$actions = array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
);
// Manage actions depending on article's status.
if ($article->isPublished()) {
$actions['draft']['template'] = 'AppBundle:ArticleAdmin:list__action_draft.html.twig';
} else {
$actions['publish']['template'] = 'AppBundle:ArticleAdmin:list__action_preview.html.twig';
}
$listMapper->add('_actions', null, array('actions' => $actions));
}
但是 $this->getSubjet()
总是 returns NULL。我也尝试了 $listMapper->getAdmin()->getSubject()
和许多其他吸气剂,但结果总是一样。
我做错了什么?
感谢阅读,祝你有美好的一天:)
您可以直接在 _action 模板中进行检查,因为您可以访问当前主题。
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('title')
->add('_action', 'actions', array(
'actions' => array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
'draft' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_draft.html.twig',
),
'publish' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_publish.html.twig',
),
))
;
}
例如在AppBundle:ArticleAdmin:list__action_draft.html.twig
,你可以检查你的情况:
{% if object.isPublished %}
your html code
{% endif %}
我在我的 Symfony 项目中使用 Sonata Admin Bundle 并为我的 Article 实体创建了一个 ArticleAdmin class。 在列表页面,我添加了一些自定义操作来快速发布、取消发布、删除和预览每篇文章。
我想做的是在文章已发布时隐藏发布按钮,反之亦然。
为此,我需要访问方法 configureListFields() 中的每个对象。我会做这样的事情:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('title');
// ...
/** @var Article article */
$article = $this->getSubject();
// Actions for all items.
$actions = array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
);
// Manage actions depending on article's status.
if ($article->isPublished()) {
$actions['draft']['template'] = 'AppBundle:ArticleAdmin:list__action_draft.html.twig';
} else {
$actions['publish']['template'] = 'AppBundle:ArticleAdmin:list__action_preview.html.twig';
}
$listMapper->add('_actions', null, array('actions' => $actions));
}
但是 $this->getSubjet()
总是 returns NULL。我也尝试了 $listMapper->getAdmin()->getSubject()
和许多其他吸气剂,但结果总是一样。
我做错了什么?
感谢阅读,祝你有美好的一天:)
您可以直接在 _action 模板中进行检查,因为您可以访问当前主题。
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('title')
->add('_action', 'actions', array(
'actions' => array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
'draft' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_draft.html.twig',
),
'publish' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_publish.html.twig',
),
))
;
}
例如在AppBundle:ArticleAdmin:list__action_draft.html.twig
,你可以检查你的情况:
{% if object.isPublished %}
your html code
{% endif %}