SonataAdminExtension.php 6.5 KB

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