BaseApplicationExtension.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * {@inheritdoc}
  17. */
  18. public function initRuntime(\Twig_Environment $environment)
  19. {
  20. $this->environment = $environment;
  21. }
  22. /**
  23. * Returns a list of filters to add to the existing list.
  24. *
  25. * @return array An array of filters
  26. */
  27. public function getFilters()
  28. {
  29. return array(
  30. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  31. 'render_form_element' => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
  32. 'render_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  33. );
  34. }
  35. public function getTokenParsers()
  36. {
  37. return array(
  38. );
  39. }
  40. /**
  41. * Returns the name of the extension.
  42. *
  43. * @return string The extension name
  44. */
  45. public function getName()
  46. {
  47. return 'base_application';
  48. }
  49. public function renderListElement($object, $field_description, $params = array())
  50. {
  51. $value = null;
  52. if(isset($field_description['reflection'])) {
  53. $value = $field_description['reflection']->getValue($object);
  54. } else if(method_exists($object, $field_description['code'])) {
  55. $value = call_user_func(array($object, $field_description['code']));
  56. }
  57. $template = $this->environment->loadTemplate($field_description['template']);
  58. return $template->render(array_merge($params, array(
  59. 'object' => $object,
  60. 'value' => $value,
  61. 'field_description' => $field_description
  62. )));
  63. }
  64. public function renderFilterElement($filter, $params = array())
  65. {
  66. $description = $filter->getDescription();
  67. $template = $this->environment->loadTemplate($description['template']);
  68. return $template->render(array_merge($params, array(
  69. 'filter' => $filter
  70. )));
  71. }
  72. public function renderFormElement($field_description, $form, $object, $params = array())
  73. {
  74. if(!isset($field_description['fieldName'])) {
  75. return '';
  76. }
  77. $field = $form->get($field_description['fieldName']);
  78. if($field->isHidden()) {
  79. return '';
  80. }
  81. $template = $this->environment->loadTemplate($field_description['template']);
  82. return $template->render(array_merge($params, array(
  83. 'object' => $object,
  84. 'field_description' => $field_description,
  85. 'field_element' => $form->get($field_description['fieldName']),
  86. )));
  87. }
  88. public function setTemplating($templating)
  89. {
  90. $this->templating = $templating;
  91. }
  92. public function getTemplating()
  93. {
  94. return $this->templating;
  95. }
  96. }