BaseApplicationExtension.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of sonata-project.
  4. *
  5. * (c) 2010 Thomas Rabaix
  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\Sonata\BaseApplicationBundle\Twig\Extension;
  11. use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
  12. class BaseApplicationExtension extends \Twig_Extension
  13. {
  14. protected $templating;
  15. /**
  16. * Returns a list of filters to add to the existing list.
  17. *
  18. * @return array An array of filters
  19. */
  20. public function getFilters()
  21. {
  22. return array(
  23. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  24. 'render_form_element' => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
  25. 'render_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  26. );
  27. }
  28. public function getTokenParsers()
  29. {
  30. return array(
  31. );
  32. }
  33. /**
  34. * Returns the name of the extension.
  35. *
  36. * @return string The extension name
  37. */
  38. public function getName()
  39. {
  40. return 'base_application';
  41. }
  42. public function renderListElement($object, $field_description, $params = array())
  43. {
  44. $value = null;
  45. if(isset($field_description['reflection'])) {
  46. $value = $field_description['reflection']->getValue($object);
  47. } else if(method_exists($object, $field_description['code'])) {
  48. $value = call_user_func(array($object, $field_description['code']));
  49. }
  50. return $this->templating->render($field_description['template'], array_merge($params, array(
  51. 'object' => $object,
  52. 'value' => $value,
  53. 'field_description' => $field_description
  54. )));
  55. }
  56. public function renderFilterElement($filter, $params = array())
  57. {
  58. $description = $filter->getDescription();
  59. return $this->templating->render($description['template'], array_merge($params, array(
  60. 'filter' => $filter
  61. )));
  62. }
  63. public function renderFormElement($field_description, $form, $object, $params = array())
  64. {
  65. if(!isset($field_description['fieldName'])) {
  66. return '';
  67. }
  68. $field = $form->get($field_description['fieldName']);
  69. if($field->isHidden()) {
  70. return '';
  71. }
  72. return $this->templating->render($field_description['template'], array_merge($params, array(
  73. 'object' => $object,
  74. 'field_description' => $field_description,
  75. 'field_element' => $form->get($field_description['fieldName']),
  76. )));
  77. }
  78. public function setTemplating($templating)
  79. {
  80. $this->templating = $templating;
  81. }
  82. public function getTemplating()
  83. {
  84. return $this->templating;
  85. }
  86. }