SonataAdminExtension.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Exception\NoValueException;
  13. use Sonata\AdminBundle\Filter\FilterInterface;
  14. use Symfony\Component\Form\FormView;
  15. class SonataAdminExtension extends \Twig_Extension
  16. {
  17. /**
  18. * @var \Twig_Environment
  19. */
  20. protected $environment;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function initRuntime(\Twig_Environment $environment)
  25. {
  26. $this->environment = $environment;
  27. }
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function getFilters()
  32. {
  33. return array(
  34. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  35. 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
  36. 'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement', array('is_safe' => array('html'))),
  37. );
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getTokenParsers()
  43. {
  44. return array();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function getName()
  50. {
  51. return 'sonata_admin';
  52. }
  53. /**
  54. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  55. * @param string $default
  56. *
  57. * @return \Twig_TemplateInterface
  58. */
  59. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
  60. {
  61. // todo: find a better solution
  62. $templateName = $fieldDescription->getTemplate() ? : $default;
  63. try {
  64. $template = $this->environment->loadTemplate($templateName);
  65. } catch (\Twig_Error_Loader $e) {
  66. $template = $this->environment->loadTemplate($default);
  67. }
  68. return $template;
  69. }
  70. /**
  71. * render a list element from the FieldDescription
  72. *
  73. * @param mixed $object
  74. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  75. * @param array $params
  76. *
  77. * @return string
  78. */
  79. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  80. {
  81. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
  82. return $this->output($fieldDescription, $template, array_merge($params, array(
  83. 'admin' => $fieldDescription->getAdmin(),
  84. 'object' => $object,
  85. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  86. 'field_description' => $fieldDescription
  87. )));
  88. }
  89. /**
  90. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  91. * @param \Twig_TemplateInterface $template
  92. * @param array $parameters
  93. *
  94. * @return string
  95. */
  96. public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array())
  97. {
  98. $content = $template->render($parameters);
  99. if ($this->environment->isDebug()) {
  100. return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
  101. $fieldDescription->getFieldName(),
  102. $fieldDescription->getTemplate(),
  103. $template->getTemplateName(),
  104. $content,
  105. $fieldDescription->getFieldName()
  106. );
  107. }
  108. return $content;
  109. }
  110. /**
  111. * return the value related to FieldDescription, if the associated object does no
  112. * exists => a temporary one is created
  113. *
  114. * @param object $object
  115. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  116. * @param array $params
  117. *
  118. * @return mixed
  119. */
  120. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  121. {
  122. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  123. throw new \RuntimeException('remove the loop requirement');
  124. }
  125. $value = null;
  126. try {
  127. $value = $fieldDescription->getValue($object);
  128. } catch (NoValueException $e) {
  129. if ($fieldDescription->getAssociationAdmin()) {
  130. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  131. }
  132. }
  133. return $value;
  134. }
  135. /**
  136. * render a view element
  137. *
  138. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  139. * @param mixed $object
  140. *
  141. * @return string
  142. */
  143. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  144. {
  145. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  146. try {
  147. $value = $fieldDescription->getValue($object);
  148. } catch (NoValueException $e) {
  149. $value = null;
  150. }
  151. return $this->output($fieldDescription, $template, array(
  152. 'field_description' => $fieldDescription,
  153. 'object' => $object,
  154. 'value' => $value,
  155. 'admin' => $fieldDescription->getAdmin()
  156. ));
  157. }
  158. /**
  159. * @throws \RunTimeException
  160. *
  161. * @param mixed $element
  162. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  163. *
  164. * @return mixed
  165. */
  166. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  167. {
  168. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  169. if (!is_object($element)) {
  170. return $element;
  171. }
  172. if (!method_exists($element, $method)) {
  173. throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option %s from service %s is ', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
  174. }
  175. return call_user_func(array($element, $method));
  176. }
  177. }