SonataAdminExtension.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Sonata\AdminBundle\Twig\Extension;
  11. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  12. use Sonata\AdminBundle\Filter\FilterInterface;
  13. use Symfony\Component\Form\FormView;
  14. class SonataAdminExtension extends \Twig_Extension
  15. {
  16. protected $templating;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function initRuntime(\Twig_Environment $environment)
  21. {
  22. $this->environment = $environment;
  23. }
  24. /**
  25. * Returns a list of filters to add to the existing list.
  26. *
  27. * @return array An array of filters
  28. */
  29. public function getFilters()
  30. {
  31. return array(
  32. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  33. 'render_form_element' => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
  34. 'render_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  35. );
  36. }
  37. public function getTokenParsers()
  38. {
  39. return array(
  40. );
  41. }
  42. /**
  43. * Returns the name of the extension.
  44. *
  45. * @return string The extension name
  46. */
  47. public function getName()
  48. {
  49. return 'sonata_admin';
  50. }
  51. /**
  52. * render a list element from the FieldDescription
  53. *
  54. * @param $object
  55. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  56. * @param array $params
  57. * @return
  58. */
  59. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  60. {
  61. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  62. return $this->output($fieldDescription, $template->render(array_merge($params, array(
  63. 'admin' => $fieldDescription->getAdmin(),
  64. 'object' => $object,
  65. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  66. 'field_description' => $fieldDescription
  67. ))));
  68. }
  69. public function output(FieldDescriptionInterface $fieldDescription, $content)
  70. {
  71. return sprintf("\n<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
  72. }
  73. /**
  74. * return the value related to FieldDescription, if the associated object does no
  75. * exists => a temporary one is created
  76. *
  77. * @param object $object
  78. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  79. * @param array $params
  80. * @return mixed
  81. */
  82. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  83. {
  84. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  85. throw new \RuntimeException('remove the loop requirement');
  86. // $object = $object[$params['loop']['index0']];
  87. }
  88. $value = $fieldDescription->getValue($object);
  89. // no value defined, check if the fieldDescription point to an association
  90. // if so, create an empty object instance
  91. // fixme: not sure this is the best place to do that
  92. if (!$value && $fieldDescription->getAssociationAdmin()) {
  93. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  94. }
  95. return $value;
  96. }
  97. /**
  98. * render a filter element
  99. *
  100. * @param Filter $filter
  101. * @param array $params
  102. * @return
  103. */
  104. public function renderFilterElement(FilterInterface $filter, array $params = array())
  105. {
  106. $description = $filter->getFieldDescription();
  107. $template = $this->environment->loadTemplate($description->getTemplate());
  108. return $template->render(array_merge($params, array(
  109. 'filter' => $filter,
  110. 'filter_form' => $filter->getField()->getForm()->createView()
  111. )));
  112. }
  113. /**
  114. * render a field element from the FieldDescription
  115. *
  116. *
  117. * @throws InvalidArgumentException
  118. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  119. * @param \Sumfony\Component\Form\FormView $formView
  120. * @param $object
  121. * @param array $params
  122. * @return string
  123. */
  124. public function renderFormElement(FieldDescriptionInterface $fieldDescription, FormView $formView, $object, $params = array())
  125. {
  126. if (!$fieldDescription->getFieldName()) {
  127. return '';
  128. }
  129. if (!$formView->offsetExists($fieldDescription->getFieldName())) {
  130. throw new \RuntimeException(sprintf('No child named %s', $fieldDescription->getFieldName()));
  131. }
  132. $children = $formView->offsetGet($fieldDescription->getFieldName());
  133. if (in_array('hidden', $children->get('types'))) {
  134. return '';
  135. }
  136. // find the correct edit parameter
  137. // edit : standard | inline
  138. // inline : natural | table
  139. $parentFieldDescription = $fieldDescription->getAdmin()->getParentFieldDescription();
  140. if (!$parentFieldDescription) {
  141. $params['edit'] = $fieldDescription->getOption('edit', 'standard');
  142. $params['inline'] = $fieldDescription->getOption('inline', 'natural');
  143. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', 'standard');
  144. } else {
  145. $params['edit'] = $parentFieldDescription->getOption('edit', 'standard');
  146. $params['inline'] = $parentFieldDescription->getOption('inline', 'natural');
  147. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', $params['edit']);
  148. }
  149. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  150. return $this->output($fieldDescription, $template->render(array_merge($params, array(
  151. 'admin' => $fieldDescription->getAdmin(),
  152. 'object' => $object,
  153. 'field_description' => $fieldDescription,
  154. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription, $params),
  155. 'field_element' => $children,
  156. 'base_template' => $fieldDescription->getOption('base_template', $base_template)
  157. ))));
  158. }
  159. /**
  160. * set the templating engine
  161. *
  162. * @param $templating
  163. * @return void
  164. */
  165. public function setTemplating($templating)
  166. {
  167. $this->templating = $templating;
  168. }
  169. /**
  170. * return the templating engine
  171. *
  172. * @return Engine
  173. */
  174. public function getTemplating()
  175. {
  176. return $this->templating;
  177. }
  178. }