Преглед на файлове

Merge pull request #773 from Romain-Geissler/fix-order-non-displayed-field

Build a non displayed list field if the sort field is not displayed.
Thomas преди 13 години
родител
ревизия
1da152ce97
променени са 5 файла, в които са добавени 82 реда и са изтрити 9 реда
  1. 18 1
      Admin/Admin.php
  2. 11 0
      Builder/ListBuilderInterface.php
  3. 21 6
      Datagrid/Datagrid.php
  4. 31 1
      Resources/doc/reference/advance.rst
  5. 1 1
      Resources/views/CRUD/base_list.html.twig

+ 18 - 1
Admin/Admin.php

@@ -689,8 +689,25 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             return;
         }
 
+        $filterParameters = $this->getFilterParameters();
+
+        // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
+        if (isset($filterParameters['_sort_by']) && is_string($filterParameters['_sort_by'])) {
+            if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
+                $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
+            } else {
+                $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
+                    $this->getClass(),
+                    $filterParameters['_sort_by'],
+                    array()
+                );
+
+                $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
+            }
+        }
+
         // initialize the datagrid
-        $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $this->getFilterParameters());
+        $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters);
 
         $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
         $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);

+ 11 - 0
Builder/ListBuilderInterface.php

@@ -29,6 +29,17 @@ interface ListBuilderInterface
     function getBaseList(array $options = array());
 
     /**
+     * Modify a field description to display it in the list view.
+     *
+     * @param null|mixed                                           $type
+     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface  $fieldDescription
+     * @param \Sonata\AdminBundle\Admin\AdminInterface             $admin
+     */
+    function buildField($type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin);
+
+    /**
+     * Modify a field description and add it to the displayed columns.
+     *
      * @abstract
      *
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list

+ 21 - 6
Datagrid/Datagrid.php

@@ -15,8 +15,11 @@ use Sonata\AdminBundle\Datagrid\PagerInterface;
 use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Sonata\AdminBundle\Filter\FilterInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
+use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 
 use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\Exception\UnexpectedTypeException;
+use Symfony\Component\Form\CallbackTransformer;
 
 class Datagrid implements DatagridInterface
 {
@@ -97,6 +100,18 @@ class Datagrid implements DatagridInterface
         }
 
         $this->formBuilder->add('_sort_by', 'hidden');
+        $this->formBuilder->get('_sort_by')->appendClientTransformer(new CallbackTransformer(
+            function($value) {
+                return $value;
+            },
+            function($value) {
+                if ($value instanceof FieldDescriptionInterface) {
+                    return $value->getName();
+                } else {
+                    return $value;
+                }
+            }
+        ));
         $this->formBuilder->add('_sort_order', 'hidden');
         $this->formBuilder->add('_page', 'hidden');
 
@@ -111,13 +126,13 @@ class Datagrid implements DatagridInterface
         }
 
         if (isset($this->values['_sort_by'])) {
-            foreach ($this->getColumns()->getElements() as $fieldDescription){
-                if ($fieldDescription->isSortable() && $fieldDescription->getName() == $this->values['_sort_by']) {
-                    $this->query->setSortBy($fieldDescription->getParentAssociationMappings(), $fieldDescription->getSortFieldMapping());
-                    $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
+            if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
+                throw new UnexpectedTypeException($this->values['_sort_by'],'FieldDescriptionInterface');
+            }
 
-                    break;
-                }
+            if ($this->values['_sort_by']->isSortable()) {
+                $this->query->setSortBy($this->values['_sort_by']->getParentAssociationMappings(), $this->values['_sort_by']->getSortFieldMapping());
+                $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
             }
         }
 

+ 31 - 1
Resources/doc/reference/advance.rst

@@ -86,4 +86,34 @@ application's config file:
 Admin Extension
 ---------------
 
-S
+Configure the default page and ordering in the list view
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Configuring the default page and ordering column can simply be achieved by overriding
+the ``datagridValues`` array property. All three keys ``_page``, ``_sort_order`` and
+``_sort_by`` can be omitted.
+
+.. code-block:: php
+
+    <?php
+
+    use Sonata\AdminBundle\Admin\Admin;
+
+    class PageAdmin extends Admin
+    {
+        // ...
+
+        /**
+         * Default Datagrid values
+         *
+         * @var array
+         */
+        protected $datagridValues = array(
+            '_page' => 1, // Display the first page (default = 1)
+            '_sort_order' => 'DESC', // Descendant ordering (default = 'ASC')
+            '_sort_by' => 'updated' // name of the ordered field (default = the model id field, if any)
+            // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
+        );
+
+        // ...
+    }

+ 1 - 1
Resources/views/CRUD/base_list.html.twig

@@ -40,7 +40,7 @@ file that was distributed with this source code.
                                     {% set sortable = false %}
                                     {% if field_description.options.sortable is defined and field_description.options.sortable%}
                                         {% set sortable             = true %}
-                                        {% set current              = admin.datagrid.values._sort_by == field_description.name %}
+                                        {% set current              = admin.datagrid.values._sort_by == field_description %}
                                         {% set sort_parameters      = admin.modelmanager.sortparameters(field_description, admin.datagrid) %}
                                         {% set sort_active_class    = current ? 'sonata-ba-list-field-order-active' : '' %}
                                         {% set sort_by              = current ? admin.datagrid.values._sort_order : field_description.options._sort_order %}