Selaa lähdekoodia

Added options to admin_type to allow to hide delete

Hugo Briand 10 vuotta sitten
vanhempi
commit
480ae9030c
2 muutettua tiedostoa jossa 29 lisäystä ja 6 poistoa
  1. 13 1
      Form/Type/AdminType.php
  2. 16 5
      Resources/doc/reference/form_types.rst

+ 13 - 1
Form/Type/AdminType.php

@@ -30,12 +30,17 @@ class AdminType extends AbstractType
     public function buildForm(FormBuilderInterface $builder, array $options)
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
     {
         $admin = clone $this->getAdmin($options);
         $admin = clone $this->getAdmin($options);
+
         if ($admin->hasParentFieldDescription()) {
         if ($admin->hasParentFieldDescription()) {
             $admin->getParentFieldDescription()->setAssociationAdmin($admin);
             $admin->getParentFieldDescription()->setAssociationAdmin($admin);
         }
         }
 
 
         if ($options['delete'] && $admin->isGranted('DELETE')) {
         if ($options['delete'] && $admin->isGranted('DELETE')) {
-            $builder->add('_delete', 'checkbox', array('required' => false, 'mapped' => false, 'translation_domain' => $admin->getTranslationDomain()));
+            if (!array_key_exists('translation_domain', $options['delete_options']['type_options'])) {
+                $options['delete_options']['type_options']['translation_domain'] = $admin->getTranslationDomain();
+            }
+
+            $builder->add('_delete', $options['delete_options']['type'], $options['delete_options']['type_options']);
         }
         }
 
 
         $admin->setSubject($builder->getData());
         $admin->setSubject($builder->getData());
@@ -65,6 +70,13 @@ class AdminType extends AbstractType
             'delete'          => function (Options $options) {
             'delete'          => function (Options $options) {
                 return ($options['btn_delete'] !== false);
                 return ($options['btn_delete'] !== false);
             },
             },
+            'delete_options'  => array(
+                'type'         => 'checkbox',
+                'type_options' => array(
+                    'required' => false,
+                    'mapped'   => false,
+                ),
+            ),
             'auto_initialize' => false,
             'auto_initialize' => false,
             'btn_add'         => 'link_add',
             'btn_add'         => 'link_add',
             'btn_list'        => 'link_list',
             'btn_list'        => 'link_list',

+ 16 - 5
Resources/doc/reference/form_types.rst

@@ -176,9 +176,9 @@ model_manager
 
 
 callback
 callback
   defaults to null. Callable function that can be used to modify the query which is used to retrieve autocomplete items.
   defaults to null. Callable function that can be used to modify the query which is used to retrieve autocomplete items.
-  The callback should receive three parameters - the Admin instance, the property (or properties) defined as searchable and the 
+  The callback should receive three parameters - the Admin instance, the property (or properties) defined as searchable and the
   search value entered by the user.
   search value entered by the user.
-  
+
   From the ``$admin`` parameter it is possible to get the ``Datagrid`` and the ``Request``:
   From the ``$admin`` parameter it is possible to get the ``Datagrid`` and the ``Request``:
 
 
 .. code-block:: php
 .. code-block:: php
@@ -332,8 +332,19 @@ to the underlying forms.
         {
         {
             $formMapper
             $formMapper
                 ->add('sales', 'sonata_type_collection', array(
                 ->add('sales', 'sonata_type_collection', array(
-                    // Prevents the "Delete" option from being displayed
-                    'type_options' => array('delete' => false)
+                    'type_options' => array(
+                        // Prevents the "Delete" option from being displayed
+                        'delete' => false,
+                        'delete_options' => array(
+                            // You may otherwise choose to put the field but hide it
+                            'type'         => 'hidden',
+                            // In that case, you need to fill in the options as well
+                            'type_options' => array(
+                                'mapped'   => false,
+                                'required' => false,
+                            )
+                        )
+                    )
                 ), array(
                 ), array(
                     'edit' => 'inline',
                     'edit' => 'inline',
                     'inline' => 'table',
                     'inline' => 'table',
@@ -354,7 +365,7 @@ btn_add and btn_catalogue:
 **TIP**: A jQuery event is fired after a row has been added (``sonata-admin-append-form-element``).
 **TIP**: A jQuery event is fired after a row has been added (``sonata-admin-append-form-element``).
 You can listen to this event to trigger custom javascript (eg: add a calendar widget to a newly added date field)
 You can listen to this event to trigger custom javascript (eg: add a calendar widget to a newly added date field)
 
 
-**TIP**: Setting the 'required' option to true does not cause a requirement of 'at least one' child entity. 
+**TIP**: Setting the 'required' option to true does not cause a requirement of 'at least one' child entity.
 Setting the 'required' option to false causes all nested form fields to become not required as well.
 Setting the 'required' option to false causes all nested form fields to become not required as well.
 
 
 sonata_type_native_collection (previously collection)
 sonata_type_native_collection (previously collection)