SonataAdminExtension.php 6.3 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 Sonata\AdminBundle\Admin\NoValueException;
  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. * Returns a list of filters to add to the existing list.
  30. *
  31. * @return array An array of filters
  32. */
  33. public function getFilters()
  34. {
  35. return array(
  36. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  37. 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
  38. 'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement', array('is_safe' => array('html'))),
  39. );
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function getTokenParsers()
  45. {
  46. return array();
  47. }
  48. /**
  49. * Returns the name of the extension.
  50. *
  51. * @return string The extension name
  52. */
  53. public function getName()
  54. {
  55. return 'sonata_admin';
  56. }
  57. /**
  58. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  59. * @param string $default
  60. * @return \Twig_TemplateInterface
  61. */
  62. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
  63. {
  64. // todo: find a better solution
  65. try {
  66. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  67. } catch(\Twig_Error_Loader $e) {
  68. $template = $this->environment->loadTemplate($default);
  69. }
  70. return $template;
  71. }
  72. /**
  73. * render a list element from the FieldDescription
  74. *
  75. * @param $object
  76. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  77. * @param array $params
  78. * @return string
  79. */
  80. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  81. {
  82. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
  83. return $this->output($fieldDescription, $template, array_merge($params, array(
  84. 'admin' => $fieldDescription->getAdmin(),
  85. 'object' => $object,
  86. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  87. 'field_description' => $fieldDescription
  88. )));
  89. }
  90. /**
  91. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  92. * @param \Twig_TemplateInterface $template
  93. * @param array $parameters
  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. * @return mixed
  118. */
  119. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  120. {
  121. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  122. throw new \RuntimeException('remove the loop requirement');
  123. }
  124. $value = null;
  125. try {
  126. $value = $fieldDescription->getValue($object);
  127. } catch (NoValueException $e) {
  128. if ($fieldDescription->getAssociationAdmin()) {
  129. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  130. }
  131. }
  132. return $value;
  133. }
  134. /**
  135. * render a view element
  136. *
  137. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  138. * @param mixed $object
  139. * @return string
  140. */
  141. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  142. {
  143. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  144. try {
  145. $value = $fieldDescription->getValue($object);
  146. } catch (NoValueException $e) {
  147. $value = null;
  148. }
  149. return $this->output($fieldDescription, $template, array(
  150. 'field_description' => $fieldDescription,
  151. 'object' => $object,
  152. 'value' => $value
  153. ));
  154. }
  155. /**
  156. * @throws \RunTimeException
  157. * @param $element
  158. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  159. * @return mixed
  160. */
  161. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  162. {
  163. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  164. if (!is_object($element)) {
  165. return $element;
  166. }
  167. if (!method_exists($element, $method)) {
  168. 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()));
  169. }
  170. return call_user_func(array($element, $method));
  171. }
  172. }