SonataAdminExtension.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * 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. $templateName = $fieldDescription->getTemplate() ?: $default;
  66. try {
  67. $template = $this->environment->loadTemplate($templateName);
  68. } catch(\Twig_Error_Loader $e) {
  69. $template = $this->environment->loadTemplate($default);
  70. }
  71. return $template;
  72. }
  73. /**
  74. * render a list element from the FieldDescription
  75. *
  76. * @param $object
  77. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  78. * @param array $params
  79. * @return string
  80. */
  81. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  82. {
  83. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
  84. return $this->output($fieldDescription, $template, array_merge($params, array(
  85. 'admin' => $fieldDescription->getAdmin(),
  86. 'object' => $object,
  87. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  88. 'field_description' => $fieldDescription
  89. )));
  90. }
  91. /**
  92. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  93. * @param \Twig_TemplateInterface $template
  94. * @param array $parameters
  95. * @return string
  96. */
  97. public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array())
  98. {
  99. $content = $template->render($parameters);
  100. if ($this->environment->isDebug()) {
  101. return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
  102. $fieldDescription->getFieldName(),
  103. $fieldDescription->getTemplate(),
  104. $template->getTemplateName(),
  105. $content,
  106. $fieldDescription->getFieldName()
  107. );
  108. }
  109. return $content;
  110. }
  111. /**
  112. * return the value related to FieldDescription, if the associated object does no
  113. * exists => a temporary one is created
  114. *
  115. * @param object $object
  116. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  117. * @param array $params
  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. * @return string
  141. */
  142. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  143. {
  144. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  145. try {
  146. $value = $fieldDescription->getValue($object);
  147. } catch (NoValueException $e) {
  148. $value = null;
  149. }
  150. return $this->output($fieldDescription, $template, array(
  151. 'field_description' => $fieldDescription,
  152. 'object' => $object,
  153. 'value' => $value,
  154. 'admin' => $fieldDescription->getAdmin()
  155. ));
  156. }
  157. /**
  158. * @throws \RunTimeException
  159. * @param $element
  160. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  161. * @return mixed
  162. */
  163. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  164. {
  165. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  166. if (!is_object($element)) {
  167. return $element;
  168. }
  169. if (!method_exists($element, $method)) {
  170. 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()));
  171. }
  172. return call_user_func(array($element, $method));
  173. }
  174. }