简体   繁体   中英

How to add a custom callback method in configureListFields() sonata admin?

I want to add a custom callback method on a sonata field list configureListFields()

It's possible in the configureDatagridFilters() method like below:

protected function configureDatagridFilters(DatagridMapper $filter)
{
    $filter
        ->add('user', 'doctrine_orm_callback', array(
            'callback'   => array($this, 'customMethod'),
        ));
}

But is it possible in the configureListFields() method ?

Your attempt using the doctrine_orm_callback is correct: Doctrine2 ORM Admin's documentation - Reference - Filter Field Definition (2.2) - 5.5.3. Callback

Use the provided example and replace comments with your bookings and then apply your conditions to the builder :

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('title')
        ->add('enabled')
        ->add('tags', null, array(), null, array('expanded' => true, 'multiple' => true))
        ->add('author')
        ->add('with_open_comments', 'doctrine_orm_callback', array(
            'callback' => function($queryBuilder, $alias, $field, $value) {
                if (!$value) {
                    return;
                }

                $queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c');
                $queryBuilder->andWhere('c.status = :status');
                $queryBuilder->setParameter('status', Comment::STATUS_MODERATE);

                return true;
            },
            'field_type' => 'checkbox'
        ))
    ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM