Browse Source

Refactor the ListMapper::add to match the FormMapper::add

Thomas Rabaix 14 năm trước cách đây
mục cha
commit
b6e7fde52c

+ 1 - 2
Admin/Admin.php

@@ -521,14 +521,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             $fieldDescription = $this->modelManager->getNewFieldDescriptionInstance($this->getClass(), 'batch', array(
                 'label'    => 'batch',
                 'code'     => '_batch',
-                'type'     => 'batch',
                 'sortable' => false
             ));
 
             $fieldDescription->setAdmin($this);
             $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__batch.html.twig');
 
-            $mapper->add($fieldDescription);
+            $mapper->add($fieldDescription, 'batch');
         }
 
         $this->configureListFields($mapper);

+ 2 - 2
Builder/ListBuilderInterface.php

@@ -32,7 +32,7 @@ interface ListBuilderInterface
      * @param \Sonata\AdminBundle\Admin\FieldDescription $fieldDescription
      * @return void
      */
-    function addField(FieldDescriptionCollection $list, FieldDescriptionInterface $fieldDescription);
+    function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin);
 
     /**
      * @abstract
@@ -41,5 +41,5 @@ interface ListBuilderInterface
      * @param array $options
      * @return void
      */
-    function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription, array $options = array());
+    function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription);
 }

+ 11 - 4
Builder/ORM/ListBuilder.php

@@ -26,8 +26,17 @@ class ListBuilder implements ListBuilderInterface
         return new FieldDescriptionCollection;
     }
 
-    public function addField(FieldDescriptionCollection $list, FieldDescriptionInterface $fieldDescription)
+    public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
     {
+        if ($type == null) {
+            throw new \RunTimeException('type guesser on ListBuilder is not yet implemented');
+        }
+
+        $fieldDescription->setType($type);
+
+        $this->fixFieldDescription($admin, $fieldDescription);
+        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
+
         return $list->add($fieldDescription);
     }
 
@@ -39,10 +48,8 @@ class ListBuilder implements ListBuilderInterface
      * @param array $options
      * @return void
      */
-    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription, array $options = array())
+    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
     {
-        $fieldDescription->mergeOptions($options);
-
         if ($fieldDescription->getName() == '_action') {
             $this->buildActionFieldDescription($fieldDescription);
         }

+ 2 - 11
Datagrid/ListMapper.php

@@ -41,7 +41,7 @@ class ListMapper
      * @param array $fieldDescriptionOptions
      * @return \Sonata\AdminBundle\Datagrid\ListMapper
      */
-    public function add($name, array $fieldDescriptionOptions = array())
+    public function add($name, $type = null, array $fieldDescriptionOptions = array())
     {
         if ($name instanceof FieldDescriptionInterface) {
 
@@ -56,21 +56,12 @@ class ListMapper
                 $fieldDescriptionOptions
             );
 
-            $this->listBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
-            $this->admin->addListFieldDescription($name, $fieldDescription);
-
-        } else if (is_string($name) && $this->admin->hasListFieldDescription($name)) {
-            $fieldDescription = $this->admin->getListFieldDescription($name);
         } else {
-
             throw new \RuntimeException('invalid state');
         }
 
         // add the field with the FormBuilder
-        $this->listBuilder->addField(
-            $this->list,
-            $fieldDescription
-        );
+        $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
 
         return $this;
     }

+ 1 - 0
Resources/views/CRUD/base_filter_field.html.twig

@@ -9,6 +9,7 @@ file that was distributed with this source code.
 
 #}
 
+
 <div>
     {% block label %}
         {% if filter.fielddescription.options.name is defined %}