소스 검색

Merge pull request #1759 from pulzarraider/sonata_type_model_hidden

Added sonata_type_model_hidden form type
Thomas 11 년 전
부모
커밋
1af21e442e
4개의 변경된 파일151개의 추가작업 그리고 4개의 파일을 삭제
  1. 66 0
      Form/Type/ModelHiddenType.php
  2. 4 0
      Resources/config/form_types.xml
  3. 34 4
      Resources/doc/reference/form_types.rst
  4. 47 0
      Tests/Form/Type/ModelHiddenTypeTest.php

+ 66 - 0
Form/Type/ModelHiddenType.php

@@ -0,0 +1,66 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\AdminBundle\Form\Type;
+
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\OptionsResolver\Options;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
+
+/**
+ * This type define a standard hidden field, that stored id to a object
+ *
+ * @author Andrej Hudec <pulzarraider@gmail.com>
+ *
+ */
+class ModelHiddenType extends AbstractType
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
+        ;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setDefaultOptions(OptionsResolverInterface $resolver)
+    {
+        $resolver->setDefaults(array(
+            'model_manager'     => null,
+            'class'             => null,
+        ));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getParent()
+    {
+        return 'hidden';
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getName()
+    {
+        return 'sonata_type_model_hidden';
+    }
+}

+ 4 - 0
Resources/config/form_types.xml

@@ -27,6 +27,10 @@
             <tag name="form.type" alias="sonata_type_model_reference" />
             <tag name="form.type" alias="sonata_type_model_reference" />
         </service>
         </service>
 
 
+        <service id="sonata.admin.form.type.model_hidden" class="Sonata\AdminBundle\Form\Type\ModelHiddenType">
+            <tag name="form.type" alias="sonata_type_model_hidden" />
+        </service>
+
         <service id="sonata.admin.form.type.array" class="Sonata\AdminBundle\Form\Type\ImmutableArrayType">
         <service id="sonata.admin.form.type.array" class="Sonata\AdminBundle\Form\Type\ImmutableArrayType">
             <tag name="form.type" alias="sonata_type_immutable_array" />
             <tag name="form.type" alias="sonata_type_immutable_array" />
         </service>
         </service>

+ 34 - 4
Resources/doc/reference/form_types.rst

@@ -82,10 +82,40 @@ class
 
 
 btn_add, btn_list, btn_delete and btn_catalogue:
 btn_add, btn_list, btn_delete and btn_catalogue:
   The labels on the ``add``, ``list`` and ``delete`` buttons can be customized
   The labels on the ``add``, ``list`` and ``delete`` buttons can be customized
-  with these parameters. Setting any of them to ``false`` will hide the 
+  with these parameters. Setting any of them to ``false`` will hide the
   corresponding button. You can also specify a custom translation catalogue
   corresponding button. You can also specify a custom translation catalogue
   for these labels, which defaults to ``SonataAdminBundle``.
   for these labels, which defaults to ``SonataAdminBundle``.
 
 
+sonata_type_model_hidden
+^^^^^^^^^^^^^^^^^^^^^^^^
+Setting a field type of ``sonata_type_model_hidden`` will use an instance of
+``ModelHiddenType`` to render hidden field. The value of hidden field is
+identifier of related entity.
+
+.. code-block:: php
+
+    class PageAdmin extends Admin
+    {
+        protected function configureFormFields(FormMapper $formMapper)
+        {
+            // generate hidden form field with id of related Category entity
+            $formMapper
+                ->add('categoryId', 'sonata_type_model_hidden')
+            ;
+        }
+    }
+
+The available options are:
+
+model_manager
+  defaults to null, but is actually calculated from the linked Admin class.
+  You usually should not need to set this manually.
+
+class
+  The entity class managed by this field. Defaults to null, but is actually
+  calculated from the linked Admin class. You usually should not need to set
+  this manually.
+
 sonata_type_admin
 sonata_type_admin
 ^^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^^
 
 
@@ -152,7 +182,7 @@ delete
 
 
 btn_add, btn_list, btn_delete and btn_catalogue:
 btn_add, btn_list, btn_delete and btn_catalogue:
   The labels on the ``add``, ``list`` and ``delete`` buttons can be customized
   The labels on the ``add``, ``list`` and ``delete`` buttons can be customized
-  with these parameters. Setting any of them to ``false`` will hide the 
+  with these parameters. Setting any of them to ``false`` will hide the
   corresponding button. You can also specify a custom translation catalogue
   corresponding button. You can also specify a custom translation catalogue
   for these labels, which defaults to ``SonataAdminBundle``.
   for these labels, which defaults to ``SonataAdminBundle``.
 
 
@@ -183,12 +213,12 @@ to the underlying forms.
             ;
             ;
         }
         }
     }
     }
-    
+
 The available options (which can be passed as a third parameter to ``FormMapper::add()``) are:
 The available options (which can be passed as a third parameter to ``FormMapper::add()``) are:
 
 
 btn_add and btn_catalogue:
 btn_add and btn_catalogue:
   The label on the ``add`` button can be customized
   The label on the ``add`` button can be customized
-  with this parameters. Setting it to ``false`` will hide the 
+  with this parameters. Setting it to ``false`` will hide the
   corresponding button. You can also specify a custom translation catalogue
   corresponding button. You can also specify a custom translation catalogue
   for this label, which defaults to ``SonataAdminBundle``.
   for this label, which defaults to ``SonataAdminBundle``.
 
 

+ 47 - 0
Tests/Form/Type/ModelHiddenTypeTest.php

@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Form\Type;
+
+use Sonata\AdminBundle\Form\Type\ModelHiddenType;
+
+use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class ModelHiddenTypeTest extends TypeTestCase
+{
+    public function testGetDefaultOptions()
+    {
+        $type = new ModelHiddenType();
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $optionResolver = new OptionsResolver();
+
+        $type->setDefaultOptions($optionResolver);
+
+        $options = $optionResolver->resolve(array('model_manager' => $modelManager, 'class' => '\Foo'));
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Model\ModelManagerInterface', $options['model_manager']);
+        $this->assertEquals($modelManager, $options['model_manager']);
+        $this->assertEquals('\Foo', $options['class']);
+    }
+
+    public function testGetName()
+    {
+        $type = new ModelHiddenType();
+        $this->assertEquals('sonata_type_model_hidden', $type->getName());
+    }
+
+    public function testGetParent()
+    {
+        $type = new ModelHiddenType();
+        $this->assertEquals('hidden', $type->getParent());
+    }
+}