SonataAdminExtension.php 6.1 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 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. public function getTokenParsers()
  42. {
  43. return array();
  44. }
  45. /**
  46. * Returns the name of the extension.
  47. *
  48. * @return string The extension name
  49. */
  50. public function getName()
  51. {
  52. return 'sonata_admin';
  53. }
  54. /**
  55. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  56. * @param string $default
  57. * @return \Twig_TemplateInterface
  58. */
  59. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
  60. {
  61. // todo: find a better solution
  62. try {
  63. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  64. } catch(\Twig_Error_Loader $e) {
  65. $template = $this->environment->loadTemplate($default);
  66. }
  67. return $template;
  68. }
  69. /**
  70. * render a list element from the FieldDescription
  71. *
  72. * @param $object
  73. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  74. * @param array $params
  75. * @return string
  76. */
  77. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  78. {
  79. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
  80. return $this->output($fieldDescription, $template, array_merge($params, array(
  81. 'admin' => $fieldDescription->getAdmin(),
  82. 'object' => $object,
  83. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  84. 'field_description' => $fieldDescription
  85. )));
  86. }
  87. /**
  88. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  89. * @param \Twig_TemplateInterface $template
  90. * @param array $parameters
  91. * @return string
  92. */
  93. public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array())
  94. {
  95. $content = $template->render($parameters);
  96. if ($this->environment->isDebug()) {
  97. return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
  98. $fieldDescription->getFieldName(),
  99. $fieldDescription->getTemplate(),
  100. $template->getTemplateName(),
  101. $content,
  102. $fieldDescription->getFieldName()
  103. );
  104. }
  105. return $content;
  106. }
  107. /**
  108. * return the value related to FieldDescription, if the associated object does no
  109. * exists => a temporary one is created
  110. *
  111. * @param object $object
  112. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  113. * @param array $params
  114. * @return mixed
  115. */
  116. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  117. {
  118. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  119. throw new \RuntimeException('remove the loop requirement');
  120. }
  121. $value = null;
  122. try {
  123. $value = $fieldDescription->getValue($object);
  124. } catch (NoValueException $e) {
  125. if ($fieldDescription->getAssociationAdmin()) {
  126. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  127. }
  128. }
  129. return $value;
  130. }
  131. /**
  132. * render a view element
  133. *
  134. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  135. * @param mixed $object
  136. * @return string
  137. */
  138. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  139. {
  140. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  141. try {
  142. $value = $fieldDescription->getValue($object);
  143. } catch (NoValueException $e) {
  144. $value = null;
  145. }
  146. return $this->output($fieldDescription, $template, array(
  147. 'field_description' => $fieldDescription,
  148. 'object' => $object,
  149. 'value' => $value
  150. ));
  151. }
  152. /**
  153. * @throws \RunTimeException
  154. * @param $element
  155. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  156. * @return mixed
  157. */
  158. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  159. {
  160. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  161. if (!method_exists($element, $method)) {
  162. 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()));
  163. }
  164. return call_user_func(array($element, $method));
  165. }
  166. }