CallbackFilter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\AdminBundle\Filter\ORM;
  11. use Symfony\Component\Form\FormFactory;
  12. use Doctrine\ORM\QueryBuilder;
  13. class CallbackFilter extends Filter
  14. {
  15. protected function association($queryBuilder, $value)
  16. {
  17. return array($queryBuilder->getRootAlias(), false);
  18. }
  19. public function filter($queryBuilder, $alias, $field, $value)
  20. {
  21. if (!is_callable($this->getOption('filter'))) {
  22. throw new \RuntimeException('Please provide a valid callback option');
  23. }
  24. call_user_func($this->getOption('filter'), $queryBuilder, $alias, $field, $value);
  25. }
  26. /**
  27. * $this->filter_fields['custom'] = array(
  28. * 'type' => 'callback',
  29. * 'filter_options' => array(
  30. * 'filter' => array($this, 'getCustomFilter'),
  31. * 'type' => 'type_name'
  32. * )
  33. * );
  34. *
  35. * @return void
  36. */
  37. public function getDefaultOptions()
  38. {
  39. return array(
  40. 'filter' => null,
  41. 'type' => 'text',
  42. );
  43. }
  44. public function defineFieldBuilder(FormFactory $formFactory)
  45. {
  46. $options = $this->getFieldDescription()->getOption('filter_field_options', array());
  47. $this->field = $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null, $options)->getForm();
  48. }
  49. }