Browse Source

Add ImmutableArrayType

Thomas Rabaix 14 years ago
parent
commit
278c0dc92a

+ 41 - 0
Form/Type/ImmutableArrayType.php

@@ -0,0 +1,41 @@
+<?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\AbstractType;
+use Symfony\Component\Form\FormBuilder;
+
+use Sonata\AdminBundle\Form\EventListener\ResizeFormListener;
+
+class ImmutableArrayType extends AbstractType
+{
+    public function buildForm(FormBuilder $builder, array $options)
+    {
+        foreach($options['keys'] as $infos) {
+            list($name, $type, $options) = $infos;
+
+            $builder->add($name, $type, $options);
+        }
+    }
+
+    public function getDefaultOptions(array $options)
+    {
+        return array(
+            'keys'    => array(),
+        );
+    }
+
+    public function getName()
+    {
+        return 'sonata_type_immutable_array';
+    }
+}

+ 4 - 0
Resources/config/form_types.xml

@@ -17,6 +17,10 @@
             <tag name="form.type" alias="sonata_type_model" />
         </service>
 
+        <service id="sonata.admin.form.type.array" class="Sonata\AdminBundle\Form\Type\ImmutableArrayType">
+            <tag name="form.type" alias="sonata_type_immutable_array" />
+        </service>
+
     </services>
 
 </container>

BIN
Resources/doc/images/sonata_type_immutable_array.png


+ 56 - 0
Resources/doc/reference/form_types_and_transformers.rst

@@ -9,12 +9,68 @@ Form types
 
     - ``sonata_type_admin`` : this type is linked to an Admin class and the field construction is
       delegated to an Admin class.
+
     - ``sonata_type_collection`` : this type works like the native ``CollectionType`` but contains two extra
       features : the data layer is abstracted to work with any implemented layer and a delete option is added
       so a collection entry can be deleted.
+
     - ``sonata_type_model`` : this type works like the native ``EntityType`` but this internal is abstracted
       to work with any implemented layer.
 
+    - ``sonata_type_immutable_array``: this type allows to edit a fixed array, like a settings array.
+
+Let's say, the object have a settings properties
+
+.. code-block:: php
+
+    <?php
+    class Page {
+        public $settings = array(
+            'content'   => 'default content',
+            'public'    => true,
+            'type'      => 1
+        );
+    }
+
+Now you can edit the settings array with :
+
+.. code-block:: php
+
+    <?php
+    namespace Sonata\PageBundle\Admin;
+
+    use Sonata\AdminBundle\Admin\Admin;
+    use Sonata\AdminBundle\Route\RouteCollection;
+    use Sonata\AdminBundle\Form\FormMapper;
+    use Sonata\PageBundle\Page\Manager;
+
+    class PageAdmin extends Admin
+    {
+
+        /**
+         * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
+         * @return void
+         */
+        public function configureFormFields(FormMapper $formMapper)
+        {
+            $formMapper
+                ->add('enabled')
+                ->addType('settings', 'sonata_type_immutable_array', array(
+                'keys' => array(
+                    array('content', 'textarea', array()),
+                    array('public', 'checkbox', array()),
+                    array('type', 'choice', array('choices' => array(1 => 'type 1', 2 => 'type 2')))
+                )
+            );
+        }
+    ));
+
+the output will be :
+
+.. image:: ../images/sonata_type_immutable_array.png
+           :alt: Dashboard
+
+
 
 Datatransformer
 ---------------

+ 12 - 0
Resources/views/CRUD/edit_sonata_type_immutable_array.html.twig

@@ -0,0 +1,12 @@
+{#
+
+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.
+
+#}
+
+{% extends base_template %}