AdminType.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ($options['delete'] && $admin->isGranted('DELETE')) {
  29. $builder->add('_delete', 'checkbox', array('required' => false, 'mapped' => false, 'translation_domain' => $admin->getTranslationDomain()));
  30. }
  31. $admin->setSubject($builder->getData());
  32. $admin->defineFormBuilder($builder);
  33. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function buildView(FormView $view, FormInterface $form, array $options)
  39. {
  40. $view->vars['btn_add'] = $options['btn_add'];
  41. $view->vars['btn_list'] = $options['btn_list'];
  42. $view->vars['btn_delete'] = $options['btn_delete'];
  43. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function setDefaultOptions(OptionsResolverInterface $resolver)
  49. {
  50. $resolver->setDefaults(array(
  51. 'delete' => function (Options $options) {
  52. return ($options['btn_delete'] !== false);
  53. },
  54. 'auto_initialize' => false,
  55. 'btn_add' => 'link_add',
  56. 'btn_list' => 'link_list',
  57. 'btn_delete' => 'link_delete',
  58. 'btn_catalogue' => 'SonataAdminBundle'
  59. ));
  60. }
  61. /**
  62. * @param array $options
  63. *
  64. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  65. *
  66. * @throws \RuntimeException
  67. */
  68. protected function getFieldDescription(array $options)
  69. {
  70. if (!isset($options['sonata_field_description'])) {
  71. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  72. }
  73. return $options['sonata_field_description'];
  74. }
  75. /**
  76. * @param array $options
  77. *
  78. * @return \Sonata\AdminBundle\Admin\AdminInterface
  79. */
  80. protected function getAdmin(array $options)
  81. {
  82. return $this->getFieldDescription($options)->getAssociationAdmin();
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getName()
  88. {
  89. return 'sonata_type_admin';
  90. }
  91. }