AdminType.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. */
  11. namespace Sonata\AdminBundle\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\Form\ReversedTransformer;
  17. use Symfony\Component\OptionsResolver\Options;
  18. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  19. use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
  20. class AdminType extends AbstractType
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function buildForm(FormBuilderInterface $builder, array $options)
  26. {
  27. $admin = clone $this->getAdmin($options);
  28. if ($admin->hasParentFieldDescription()) {
  29. $admin->getParentFieldDescription()->setAssociationAdmin($admin);
  30. }
  31. if ($options['delete'] && $admin->isGranted('DELETE')) {
  32. $builder->add('_delete', 'checkbox', array('required' => false, 'mapped' => false, 'translation_domain' => $admin->getTranslationDomain()));
  33. }
  34. $admin->setSubject($builder->getData());
  35. $admin->defineFormBuilder($builder);
  36. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function buildView(FormView $view, FormInterface $form, array $options)
  42. {
  43. $view->vars['btn_add'] = $options['btn_add'];
  44. $view->vars['btn_list'] = $options['btn_list'];
  45. $view->vars['btn_delete'] = $options['btn_delete'];
  46. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function setDefaultOptions(OptionsResolverInterface $resolver)
  52. {
  53. $resolver->setDefaults(array(
  54. 'delete' => function (Options $options) {
  55. return ($options['btn_delete'] !== false);
  56. },
  57. 'auto_initialize' => false,
  58. 'btn_add' => 'link_add',
  59. 'btn_list' => 'link_list',
  60. 'btn_delete' => 'link_delete',
  61. 'btn_catalogue' => 'SonataAdminBundle'
  62. ));
  63. }
  64. /**
  65. * @param array $options
  66. *
  67. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  68. *
  69. * @throws \RuntimeException
  70. */
  71. protected function getFieldDescription(array $options)
  72. {
  73. if (!isset($options['sonata_field_description'])) {
  74. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  75. }
  76. return $options['sonata_field_description'];
  77. }
  78. /**
  79. * @param array $options
  80. *
  81. * @return \Sonata\AdminBundle\Admin\AdminInterface
  82. */
  83. protected function getAdmin(array $options)
  84. {
  85. return $this->getFieldDescription($options)->getAssociationAdmin();
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getName()
  91. {
  92. return 'sonata_type_admin';
  93. }
  94. }