CallbackFilter.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\BaseApplicationBundle\Filter;
  11. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  12. use Doctrine\ORM\QueryBuilder;
  13. class CallbackFilter extends Filter
  14. {
  15. protected function association(QueryBuilder $queryBuilder, $value)
  16. {
  17. return array($queryBuilder->getRootAlias(), false);
  18. }
  19. public function filter(QueryBuilder $queryBuilder, $alias, $field, $value)
  20. {
  21. call_user_func($this->getOption('filter'), $queryBuilder, $alias, $field, $value);
  22. }
  23. /**
  24. * $this->filter_fields['custom'] = array(
  25. * 'type' => 'callback',
  26. * 'filter_options' => array(
  27. * 'filter' => array($this, 'getCustomFilter'),
  28. * 'field' => array($this, 'getCustomField')
  29. * )
  30. * );
  31. *
  32. * @return void
  33. */
  34. protected function configure()
  35. {
  36. $this->addRequiredOption('filter');
  37. $this->addRequiredOption('field');
  38. parent::configure();
  39. }
  40. public function getFormField()
  41. {
  42. return call_user_func($this->getOption('field'), $this);
  43. }
  44. }