BaseApplicationExtension.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\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. );
  26. }
  27. public function getTokenParsers()
  28. {
  29. return array(
  30. );
  31. }
  32. /**
  33. * Returns the name of the extension.
  34. *
  35. * @return string The extension name
  36. */
  37. public function getName()
  38. {
  39. return 'base_application';
  40. }
  41. public function renderListElement($object, $field_description, $params = array())
  42. {
  43. $value = null;
  44. if(isset($field_description['reflection'])) {
  45. $value = $field_description['reflection']->getValue($object);
  46. }
  47. return $this->templating->render($field_description['template'], array_merge($params, array(
  48. 'object' => $object,
  49. 'value' => $value,
  50. 'field_description' => $field_description
  51. )));
  52. }
  53. public function renderFormElement($field_description, $form, $object, $params = array())
  54. {
  55. if(!isset($field_description['fieldName'])) {
  56. return '';
  57. }
  58. $field = $form->get($field_description['fieldName']);
  59. if($field->isHidden())
  60. {
  61. return '';
  62. }
  63. return $this->templating->render($field_description['template'], array_merge($params, array(
  64. 'object' => $object,
  65. 'field_description' => $field_description,
  66. 'field_element' => $form->get($field_description['fieldName']),
  67. )));
  68. }
  69. public function setTemplating($templating)
  70. {
  71. $this->templating = $templating;
  72. }
  73. public function getTemplating()
  74. {
  75. return $this->templating;
  76. }
  77. }