CallbackFilter.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Bundle\BaseApplicationBundle\Filter;
  11. class CallbackFilter extends Filter
  12. {
  13. protected function association($query_builder, $value)
  14. {
  15. return array($query_builder->getRootAlias(), false);
  16. }
  17. public function filter($query_builder, $alias, $field, $value)
  18. {
  19. call_user_func($this->getOption('filter'), $query_builder, $alias, $field, $value);
  20. }
  21. /**
  22. * $this->filter_fields['custom'] = array(
  23. * 'type' => 'callback',
  24. * 'filter_options' => array(
  25. * 'filter' => array($this, 'getCustomFilter'),
  26. * 'field' => array($this, 'getCustomField')
  27. * )
  28. * );
  29. *
  30. * @return void
  31. */
  32. protected function configure()
  33. {
  34. $this->addRequiredOption('filter');
  35. $this->addRequiredOption('field');
  36. parent::configure();
  37. }
  38. public function getFormField()
  39. {
  40. return call_user_func($this->getOption('field'), $this);
  41. }
  42. }