浏览代码

Add ModelTypeList

Thomas Rabaix 13 年之前
父节点
当前提交
426f23ce15
共有 3 个文件被更改,包括 95 次插入4 次删除
  1. 84 0
      Form/Type/ModelTypeList.php
  2. 7 3
      Resources/config/form_types.xml
  3. 4 1
      UPGRATE-2.1.md

+ 84 - 0
Form/Type/ModelTypeList.php

@@ -0,0 +1,84 @@
+<?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\Form\FormInterface;
+use Symfony\Component\Form\FormViewInterface;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
+
+/**
+ * This type is used to render an hidden input text and 3 links
+ *   - an add form modal
+ *   - a list modal to select the targetted entities
+ *   - a clear selection link
+ */
+class ModelTypeList extends AbstractType
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->resetViewTransformers()
+            ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function buildView(FormViewInterface $view, FormInterface $form, array $options)
+    {
+        if ($view->hasVar('sonata_admin')) {
+            $parameters = $view->getVar('sonata_admin');
+
+            // set the correct edit mode
+            $parameters['edit'] = 'list';
+
+            $view->setVar('sonata_admin', $parameters);
+        }
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setDefaultOptions(OptionsResolverInterface $resolver)
+    {
+        $resolver->setDefaults(array(
+            'model_manager'     => null,
+            'class'             => null,
+            'parent'            => 'text',
+        ));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getParent()
+    {
+        return 'text';
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getName()
+    {
+        return 'sonata_type_model_list';
+    }
+}

+ 7 - 3
Resources/config/form_types.xml

@@ -15,10 +15,14 @@
             <tag name="form.type" alias="sonata_type_collection" />
         </service>
 
-        <service id="sonata.admin.form.type.model" class="Sonata\AdminBundle\Form\Type\ModelType">
+        <service id="sonata.admin.form.type.model_choice" class="Sonata\AdminBundle\Form\Type\ModelType">
             <tag name="form.type" alias="sonata_type_model" />
         </service>
 
+        <service id="sonata.admin.form.type.model_list" class="Sonata\AdminBundle\Form\Type\ModelTypeList">
+            <tag name="form.type" alias="sonata_type_model_list" />
+        </service>
+
         <service id="sonata.admin.form.type.model_reference" class="Sonata\AdminBundle\Form\Type\ModelReferenceType">
             <tag name="form.type" alias="sonata_type_model_reference" />
         </service>
@@ -36,7 +40,7 @@
             <tag name="form.type" alias="sonata_type_translatable_choice" />
             <argument type="service" id="translator" />
         </service>
-        
+
         <service id="sonata.admin.form.type.date_range" class="Sonata\AdminBundle\Form\Type\DateRangeType">
             <tag name="form.type" alias="sonata_type_date_range" />
 
@@ -76,7 +80,7 @@
         <service id="sonata.admin.form.filter.type.default" class="Sonata\AdminBundle\Form\Type\Filter\DefaultType">
             <tag name="form.type" alias="sonata_type_filter_default" />
         </service>
-        
+
         <service id="sonata.admin.form.filter.type.date" class="Sonata\AdminBundle\Form\Type\Filter\DateType">
             <tag name="form.type" alias="sonata_type_filter_date" />
             <argument type="service" id="translator" />

+ 4 - 1
UPGRATE-2.1.md

@@ -1,6 +1,8 @@
 UPGRADE FROM 2.0 to 2.1
 =======================
 
+** Work In Progress - Please do not use this code in production **
+
 ### Form
 
   * Due to some refactoring in the Form Component, some types definition have been changed
@@ -9,5 +11,6 @@ UPGRADE FROM 2.0 to 2.1
       * sonata_type_model : this type now only render a standard select widget or a list
         widget (if multiple option is set)
 
-
+      * sonata_type_model_list : this type replaces the option ``edit = list`` provided as
+        a 4th arguments on the ``sonata_type_model``