SonataAdminExtension.php 6.7 KB

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