Kaynağa Gözat

Allow dot-separated filter identifiers.

Romain Geissler 13 yıl önce
ebeveyn
işleme
dcc0885844

+ 2 - 2
Datagrid/Datagrid.php

@@ -93,7 +93,7 @@ class Datagrid implements DatagridInterface
         foreach ($this->getFilters() as $name => $filter) {
             list($type, $options) = $filter->getRenderSettings();
 
-            $this->formBuilder->add($name, $type, $options);
+            $this->formBuilder->add($filter->getFormName(), $type, $options);
         }
 
         $this->formBuilder->add('_sort_by', 'hidden');
@@ -107,7 +107,7 @@ class Datagrid implements DatagridInterface
 
         foreach ($this->getFilters() as $name => $filter) {
             $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
-            $filter->apply($this->query, $data[$name]);
+            $filter->apply($this->query, $data[$filter->getFormName()]);
         }
 
         $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);

+ 1 - 1
Datagrid/DatagridMapper.php

@@ -60,7 +60,7 @@ class DatagridMapper
             $filterOptions['field_type'] = $fieldType;
         }
 
-        $filterOptions['field_name'] = isset($filterOptions['field_name']) ? $filterOptions['field_name'] : $name;
+        $filterOptions['field_name'] = isset($filterOptions['field_name']) ? $filterOptions['field_name'] : substr(strrchr('.'.$name,'.'), 1);
 
         if ($name instanceof FieldDescriptionInterface) {
             $fieldDescription = $name;

+ 21 - 0
Filter/Filter.php

@@ -45,6 +45,19 @@ abstract class Filter implements FilterInterface
         return $this->name;
     }
 
+    /**
+     * @return string
+     */
+    public function getFormName()
+    {
+        /* Symfony default form class sadly can't handle
+           form element with dots in its name (when data
+           get bound, the default dataMapper is a PropertyPathMapper).
+           So use this trick to avoid any issue.
+        */
+        return str_replace('.','~',$this->name);
+    }
+
     /**
      * @param string $name
      * @param null $default
@@ -114,6 +127,14 @@ abstract class Filter implements FilterInterface
         return $fieldName;
     }
 
+    /**
+     * @return array of mappings
+     */
+    public function getParentAssociationMappings()
+    {
+        return $this->getOption('parent_association_mappings', array());
+    }
+
     /**
      * @param array $options
      * @return void

+ 12 - 0
Filter/FilterInterface.php

@@ -39,6 +39,13 @@ interface FilterInterface
      */
     function getName();
 
+    /**
+     * Returns the filter form name
+     * @abstract
+     * @return string
+     */
+    function getFormName();
+
     /**
      * Returns the label name
      *
@@ -89,6 +96,11 @@ interface FilterInterface
      */
     function getFieldName();
 
+    /**
+     * @return array of mappings
+     */
+    function getParentAssociationMappings();
+
     /**
      * @abstract
      * @return array

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

@@ -177,8 +177,8 @@ file that was distributed with this source code.
                         {% for filter in admin.datagrid.filters %}
                         <tr id="filter_{{ filter.name }}_row" class="filter {{ filter.isActive ? 'active' : 'inactive' }}">
                                 <td class="filter-title">{{ admin.trans(filter.label) }}</td>
-                                <td class="filter-type">{{ form_widget(form.getChild(filter.name).getChild('type')) }}</td>
-                                <td class="filter-value">{{ form_widget(form.getChild(filter.name).getChild('value')) }}</td>
+                                <td class="filter-type">{{ form_widget(form.getChild(filter.formName).getChild('type')) }}</td>
+                                <td class="filter-value">{{ form_widget(form.getChild(filter.formName).getChild('value')) }}</td>
                             </tr>
                         {% endfor %}
                     </table>

+ 1 - 1
Translator/NativeLabelTranslatorStrategy.php

@@ -18,7 +18,7 @@ class NativeLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
      */
     public function getLabel($label, $context = '', $type = '')
     {
-        $label = str_replace('_', ' ', $label);
+        $label = str_replace(array('_', '.'), ' ', $label);
         $label = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label));
 
         return ucwords(str_replace('_', ' ', $label));

+ 2 - 0
Translator/UnderscoreLabelTranslatorStrategy.php

@@ -18,6 +18,8 @@ class UnderscoreLabelTranslatorStrategy implements LabelTranslatorStrategyInterf
      */
     public function getLabel($label, $context = '', $type = '')
     {
+        $label = str_replace('.', '_', $label);
+
         return sprintf('%s.%s_%s', $context, $type, strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
     }
 }