简体   繁体   中英

SonataAdminBundle: Create custom filter type

Is there a way to create a custom filter type for SonataAdminBundle, that extends another type, for example the type doctrine_orm_callback? The idea is for re-use the custom type in other filters

public function configureDatagridFilters(DatagridMapper $datagridMapper)
{

    $datagridMapper
        ->add('field', 'doctrine_orm_callback', array(
            'callback' => function($queryBuilder, $alias, $field, $value) {
                if (!$value['value']) {
                    return;
                }

                $queryBuilder->leftJoin(sprintf('%s.field', $alias), 'field')
                    ->andWhere("field LIKE :field")
                    ->setParameter('field', "%{$value['value']}%");

                return true;
            },
            'field_type' => 'search'), null, array('pattern' => '^[A-Za-z0-9]{1,12}$'));
}

Thanks!

Check this link and try:

->add('with_open_comments', 
      'doctrine_orm_callback', 
      array('callback'   => array($this, 'yourFunction'),
            'field_type' => 'search'), 
      null, 
      array('pattern' => '^[A-Za-z0-9]{1,12}$'));

    public function yourFunction($queryBuilder, $alias, $field, $value)
    {
        if (!$value['value']) {
           return;
        }

        $queryBuilder->leftJoin(sprintf('%s.field', $alias), 'field')
                    ->andWhere("field LIKE :field")
                    ->setParameter('field', "%{$value['value']}%");

        return true;
    }
}

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