如何在 Sonata configureListFields 中检索主题? [英] How to retrieve subject in Sonata configureListFields?

查看:14
本文介绍了如何在 Sonata configureListFields 中检索主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Symfony 项目中使用 Sonata Admin Bundle,并为我的文章实体创建了一个 ArticleAdmin 类.在列表页面中,我添加了一些自定义操作来快速发布、取消发布、删除和删除.预览每篇文章.

I use Sonata Admin Bundle in my Symfony project and created an ArticleAdmin class for my Article entity. In the list page, I added some custom actions to quickly publish, unpublish, delete & preview each article.

我想要做的是在文章已经发布时隐藏发布按钮&反之亦然.

What I want to do is to hide publish button when an article is already published & vice versa.

为此,我需要访问方法 configureListFields() 中的每个对象.我会做这样的事情:

To do this, I need to have access to each object in method configureListFields(). I would do something like this:

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() 总是返回 NULL.我还尝试了 $listMapper->getAdmin()->getSubject() 和许多其他 getter,但结果总是相同.

But $this->getSubjet() always returns NULL. I also tried $listMapper->getAdmin()->getSubject() and many other getters but always the same result.

我做错了什么?

感谢阅读 &祝你有美好的一天:)

Thanks for reading & have a good day :)

推荐答案

你可以直接在_action模板中进行检查,因为你可以访问当前主题.

You can do the check directly in the _action template, as you can access the current subject.

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 中,您可以检查您的条件:

And for example in AppBundle:ArticleAdmin:list__action_draft.html.twig, you can check your condition :

{% if object.isPublished %}
    your html code
{% endif %}

这篇关于如何在 Sonata configureListFields 中检索主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆