SonataAdminExtension.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 $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<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
  70. }
  71. /**
  72. * return the value related to FieldDescription, if the associated object does no
  73. * exists => a temporary one is created
  74. *
  75. * @param object $object
  76. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  77. * @param array $params
  78. * @return mixed
  79. */
  80. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  81. {
  82. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  83. throw new \RuntimeException('remove the loop requirement');
  84. }
  85. $value = $fieldDescription->getValue($object);
  86. // no value defined, check if the fieldDescription point to an association
  87. // if so, create an empty object instance
  88. // fixme: not sure this is the best place to do that
  89. if (!$value && $fieldDescription->getAssociationAdmin()) {
  90. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  91. }
  92. return $value;
  93. }
  94. /**
  95. * render a filter element
  96. *
  97. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  98. * @param array $params
  99. * @return
  100. */
  101. public function renderFilterElement(FilterInterface $filter, array $params = array())
  102. {
  103. $description = $filter->getFieldDescription();
  104. $template = $this->environment->loadTemplate($description->getTemplate());
  105. return $template->render(array_merge($params, array(
  106. 'filter' => $filter,
  107. 'filter_form' => $filter->getField()->createView()
  108. )));
  109. }
  110. /**
  111. * render a field element from the FieldDescription
  112. *
  113. *
  114. * @throws InvalidArgumentException
  115. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  116. * @param \Sumfony\Component\Form\FormView $formView
  117. * @param $object
  118. * @param array $params
  119. * @return string
  120. */
  121. public function renderFormElement(FieldDescriptionInterface $fieldDescription, FormView $formView, $object, $params = array())
  122. {
  123. if (!$fieldDescription->getFieldName()) {
  124. return '';
  125. }
  126. if (!$formView->offsetExists($fieldDescription->getFieldName())) {
  127. throw new \RuntimeException(sprintf('No child named %s', $fieldDescription->getFieldName()));
  128. }
  129. $children = $formView->offsetGet($fieldDescription->getFieldName());
  130. if (in_array('hidden', $children->get('types'))) {
  131. return '';
  132. }
  133. // find the correct edit parameter
  134. // edit : standard | inline
  135. // inline : natural | table
  136. $parentFieldDescription = $fieldDescription->getAdmin()->getParentFieldDescription();
  137. if (!$parentFieldDescription) {
  138. $params['edit'] = $fieldDescription->getOption('edit', 'standard');
  139. $params['inline'] = $fieldDescription->getOption('inline', 'natural');
  140. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', 'standard');
  141. } else {
  142. $params['edit'] = $parentFieldDescription->getOption('edit', 'standard');
  143. $params['inline'] = $parentFieldDescription->getOption('inline', 'natural');
  144. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', $params['edit']);
  145. }
  146. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  147. return $this->output($fieldDescription, $template->render(array_merge($params, array(
  148. 'admin' => $fieldDescription->getAdmin(),
  149. 'object' => $object,
  150. 'field_description' => $fieldDescription,
  151. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription, $params),
  152. 'field_element' => $children,
  153. 'base_template' => $fieldDescription->getOption('base_template', $base_template)
  154. ))));
  155. }
  156. }