SonataAdminExtension.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  38. 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
  39. 'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement', array('is_safe' => array('html'))),
  40. );
  41. }
  42. public function getTokenParsers()
  43. {
  44. return array();
  45. }
  46. /**
  47. * Returns the name of the extension.
  48. *
  49. * @return string The extension name
  50. */
  51. public function getName()
  52. {
  53. return 'sonata_admin';
  54. }
  55. /**
  56. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  57. * @param string $default
  58. * @return \Twig_TemplateInterface
  59. */
  60. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
  61. {
  62. // todo: find a better solution
  63. try {
  64. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  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. * @return
  77. */
  78. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  79. {
  80. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
  81. return $this->output($fieldDescription, $template, array_merge($params, array(
  82. 'admin' => $fieldDescription->getAdmin(),
  83. 'object' => $object,
  84. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  85. 'field_description' => $fieldDescription
  86. )));
  87. }
  88. /**
  89. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  90. * @param string $content
  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 filter element
  133. *
  134. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  135. * @param array $params
  136. * @return string
  137. */
  138. public function renderFilterElement(FilterInterface $filter, array $params = array())
  139. {
  140. $fieldDescription = $filter->getFieldDescription();
  141. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_filter_field.html.twig');
  142. return $this->output($fieldDescription, $template, array_merge($params, array(
  143. 'filter' => $filter,
  144. 'filter_form' => $filter->getField()->createView()
  145. )));
  146. }
  147. /**
  148. * render a view element
  149. *
  150. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  151. * @param mixed $object
  152. * @return string
  153. */
  154. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  155. {
  156. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  157. try {
  158. $value = $fieldDescription->getValue($object);
  159. } catch (NoValueException $e) {
  160. $value = null;
  161. }
  162. return $this->output($fieldDescription, $template, array(
  163. 'field_description' => $fieldDescription,
  164. 'object' => $object,
  165. 'value' => $value
  166. ));
  167. }
  168. /**
  169. * @throws \RunTimeException
  170. * @param $element
  171. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  172. * @return mixed
  173. */
  174. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  175. {
  176. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  177. if (!method_exists($element, $method)) {
  178. 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()));
  179. }
  180. return call_user_func(array($element, $method));
  181. }
  182. }