/Form/Extension/Field/Type/FormTypeFieldExtension.php

https://github.com/dcsg/SonataAdminBundle · PHP · 198 lines · 113 code · 34 blank · 51 comment · 12 complexity · 8e3006087d8e0f607dcbc59fda79bdfe MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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\Form\Extension\Field\Type;
  11. use Symfony\Component\Form\AbstractTypeExtension;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormInterface;
  14. use Symfony\Component\Form\FormView;
  15. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  16. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  17. use Sonata\AdminBundle\Exception\NoValueException;
  18. class FormTypeFieldExtension extends AbstractTypeExtension
  19. {
  20. protected $defaultClasses = array();
  21. /**
  22. * @param array $defaultClasses
  23. */
  24. public function __construct(array $defaultClasses = array())
  25. {
  26. $this->defaultClasses = $defaultClasses;
  27. }
  28. /**
  29. * @param FormBuilderInterface $builder
  30. * @param array $options
  31. */
  32. public function buildForm(FormBuilderInterface $builder, array $options)
  33. {
  34. $sonataAdmin = array(
  35. 'name' => null,
  36. 'admin' => null,
  37. 'value' => null,
  38. 'edit' => 'standard',
  39. 'inline' => 'natural',
  40. 'field_description' => null,
  41. 'block_name' => false
  42. );
  43. $builder->setAttribute('sonata_admin_enabled', false);
  44. if ($options['sonata_field_description'] instanceof FieldDescriptionInterface) {
  45. $fieldDescription = $options['sonata_field_description'];
  46. $sonataAdmin['admin'] = $fieldDescription->getAdmin();
  47. $sonataAdmin['field_description'] = $fieldDescription;
  48. $sonataAdmin['name'] = $fieldDescription->getName();
  49. $sonataAdmin['edit'] = $fieldDescription->getOption('edit', 'standard');
  50. $sonataAdmin['inline'] = $fieldDescription->getOption('inline', 'natural');
  51. $sonataAdmin['block_name'] = $fieldDescription->getOption('block_name', false);
  52. $sonataAdmin['class'] = $this->getClass($builder);
  53. $builder->setAttribute('sonata_admin_enabled', true);
  54. }
  55. $builder->setAttribute('sonata_admin', $sonataAdmin);
  56. }
  57. /**
  58. * @param FormBuilderInterface $formBuilder
  59. *
  60. * @return string
  61. */
  62. protected function getClass(FormBuilderInterface $formBuilder)
  63. {
  64. foreach ($this->getTypes($formBuilder) as $type) {
  65. if (isset($this->defaultClasses[$type->getName()])) {
  66. return $this->defaultClasses[$type->getName()];
  67. }
  68. }
  69. return '';
  70. }
  71. /**
  72. * @param \Symfony\Component\Form\FormBuilderInterface $formBuilder
  73. *
  74. * @return array
  75. */
  76. protected function getTypes(FormBuilderInterface $formBuilder)
  77. {
  78. $types = array();
  79. for ($type = $formBuilder->getType(); null !== $type; $type = $type->getParent()) {
  80. array_unshift($types, $type->getInnerType());
  81. }
  82. return $types;
  83. }
  84. /**
  85. * @param FormView $view
  86. * @param FormInterface $form
  87. * @param array $options
  88. */
  89. public function buildView(FormView $view, FormInterface $form, array $options)
  90. {
  91. $sonataAdmin = $form->getConfig()->getAttribute('sonata_admin');
  92. // avoid to add extra information not required by non admin field
  93. if ($sonataAdmin && $form->getConfig()->getAttribute('sonata_admin_enabled', true)) {
  94. $sonataAdmin['value'] = $form->getData();
  95. // add a new block types, so the Admin Form element can be tweaked based on the admin code
  96. $block_prefixes = $view->vars['block_prefixes'];
  97. $baseName = str_replace('.', '_', $sonataAdmin['admin']->getCode());
  98. $baseType = $block_prefixes[count($block_prefixes) - 2];
  99. $blockSuffix = preg_replace("#^_([a-z0-9]{14})_(.++)$#", "\$2", array_pop($block_prefixes));
  100. $block_prefixes[] = sprintf('%s_%s', $baseName, $baseType);
  101. $block_prefixes[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['name'], $baseType);
  102. $block_prefixes[] = sprintf('%s_%s_%s_%s', $baseName, $sonataAdmin['name'], $baseType, $blockSuffix);
  103. if (isset($sonataAdmin['block_name']) && $sonataAdmin['block_name'] !== false) {
  104. $block_prefixes[] = $sonataAdmin['block_name'];
  105. }
  106. $view->vars['block_prefixes'] = $block_prefixes;
  107. $view->vars['sonata_admin_enabled'] = true;
  108. $view->vars['sonata_admin'] = $sonataAdmin;
  109. $attr = $view->vars['attr'];
  110. if (!isset($attr['class']) && isset($sonataAdmin['class'])) {
  111. $attr['class'] = $sonataAdmin['class'];
  112. }
  113. $view->vars['attr'] = $attr;
  114. } else {
  115. $view->vars['sonata_admin_enabled'] = false;
  116. }
  117. $view->vars['sonata_admin'] = $sonataAdmin;
  118. }
  119. /**
  120. * Returns the name of the type being extended
  121. *
  122. * @return string The name of the type being extended
  123. */
  124. public function getExtendedType()
  125. {
  126. return 'field';
  127. }
  128. /**
  129. * Sets the default options
  130. *
  131. * @param OptionsResolverInterface $resolver Options Resolver
  132. */
  133. public function setDefaultOptions(OptionsResolverInterface $resolver)
  134. {
  135. $resolver->setDefaults(array(
  136. 'sonata_admin' => null,
  137. 'sonata_field_description' => null,
  138. ));
  139. }
  140. /**
  141. * return the value related to FieldDescription, if the associated object does no
  142. * exists => a temporary one is created
  143. *
  144. * @param object $object
  145. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  146. *
  147. * @return mixed
  148. */
  149. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription)
  150. {
  151. $value = null;
  152. if (!$object) {
  153. return $value;
  154. }
  155. try {
  156. $value = $fieldDescription->getValue($object);
  157. } catch (NoValueException $e) {
  158. if ($fieldDescription->getAssociationAdmin()) {
  159. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  160. }
  161. }
  162. return $value;
  163. }
  164. }