Kaynağa Gözat

Add possibility to hide advanced filters

Quentin Somazzi 10 yıl önce
ebeveyn
işleme
4912fbc119

+ 1 - 1
Filter/Filter.php

@@ -183,7 +183,7 @@ abstract class Filter implements FilterInterface
     public function setOptions(array $options)
     {
         $this->options = array_merge(
-            array('show_filter' => null),
+            array('show_filter' => null, 'advanced_filter' => true),
             $this->getDefaultOptions(),
             $options
         );

+ 6 - 2
Form/Type/Filter/ChoiceType.php

@@ -54,8 +54,10 @@ class ChoiceType extends AbstractType
             self::TYPE_EQUAL           => $this->translator->trans('label_type_equals', array(), 'SonataAdminBundle'),
         );
 
+        $operatorChoices = $options['operator_type'] !== 'hidden' ? array('choices' => $choices) : array();
+
         $builder
-            ->add('type', 'choice', array('choices' => $choices, 'required' => false))
+            ->add('type', $options['operator_type'], array_merge(array('required' => false), $options['operator_options'], $operatorChoices))
             ->add('value', $options['field_type'], array_merge(array('required' => false), $options['field_options']))
         ;
     }
@@ -67,7 +69,9 @@ class ChoiceType extends AbstractType
     {
         $resolver->setDefaults(array(
             'field_type'       => 'choice',
-            'field_options'    => array()
+            'field_options'    => array(),
+            'operator_type'    => 'choice',
+            'operator_options' => [],
         ));
     }
 }

+ 11 - 0
Resources/doc/reference/action_list.rst

@@ -272,6 +272,17 @@ Though this ``operator_type`` is automatically detected it can be changed or eve
         ;
     }
 
+If you don't need the advanced filters, or all your ``operator_type`` are hidden, you can disable them by setting
+``advanced_filter`` to ``false``. You need to disable all advanced filters to make the button disappear.
+
+.. code-block:: php
+
+    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
+    {
+        $datagridMapper
+            ->add('bar', null, array('operator_type' => 'hidden', 'advanced_filter' => false))
+        ;
+    }
 
 Default filters
 ^^^^^^^^^^^^^^^

+ 13 - 6
Resources/views/CRUD/base_list.html.twig

@@ -235,6 +235,7 @@ file that was distributed with this source code.
 
                         <div class="row">
                             <div class="col-sm-9">
+                                {% set withAdvancedFilter = false %}
                                 {% for filter in admin.datagrid.filters %}
                                     <div class="form-group" id="filter-{{ admin.uniqid }}-{{ filter.name }}" sonata-filter="{{ (filter.options['show_filter'] is sameas(true) or filter.options['show_filter'] is null) ? 'true' : 'false' }}" style="display: {% if (filter.isActive() and filter.options['show_filter'] is null) or (filter.options['show_filter'] is sameas(true)) %}block{% else %}none{% endif %}">
                                         {% if filter.label is not sameas(false) %}
@@ -258,6 +259,10 @@ file that was distributed with this source code.
                                             </label>
                                         </div>
                                     </div>
+
+                                    {% if filter.options['advanced_filter'] %}
+                                        {% set withAdvancedFilter = true %}
+                                    {% endif %}
                                 {% endfor %}
                             </div>
                             <div class="col-sm-3 text-center">
@@ -276,12 +281,14 @@ file that was distributed with this source code.
                                     </a>
                                 </div>
 
-                                <div class="form-group">
-                                    <a href="#" data-toggle="advanced-filter">
-                                        <i class="fa fa-cogs"></i>
-                                        {{ 'btn_advanced_filters'|trans({}, 'SonataAdminBundle') }}
-                                    </a>
-                                </div>
+                                {% if withAdvancedFilter %}
+                                    <div class="form-group">
+                                        <a href="#" data-toggle="advanced-filter">
+                                            <i class="fa fa-cogs"></i>
+                                            {{ 'btn_advanced_filters'|trans({}, 'SonataAdminBundle') }}
+                                        </a>
+                                    </div>
+                                {% endif %}
                             </div>
                         </div>
 

+ 3 - 3
Tests/Admin/AdminTest.php

@@ -572,7 +572,7 @@ class AdminTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException RuntimeException
+     * @expectedException \RuntimeException
      */
     public function testGetBaseRouteNameWithUnreconizedClassname()
     {
@@ -672,7 +672,7 @@ class AdminTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException RuntimeException
+     * @expectedException \RuntimeException
      */
     public function testNonExistantSubclass()
     {
@@ -1529,7 +1529,7 @@ class AdminTest extends \PHPUnit_Framework_TestCase
                         break;
 
                     default:
-                        throw new \RuntiemException(sprintf('Unknown filter name "%s"', $name));
+                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
                         break;
                 }
 

+ 2 - 7
Tests/Datagrid/DatagridMapperTest.php

@@ -12,15 +12,8 @@
 
 namespace Sonata\AdminBundle\Tests\Datagrid;
 
-use Sonata\AdminBundle\Admin\AdminInterface;
-use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\Datagrid;
-use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
-use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
-use Sonata\AdminBundle\Datagrid\PagerInterface;
-use Sonata\AdminBundle\Filter\Filter;
-use Sonata\AdminBundle\Filter\FilterInterface;
 
 /**
  * @author Andrej Hudec <pulzarraider@gmail.com>
@@ -122,6 +115,7 @@ class DatagridMapperTest extends \PHPUnit_Framework_TestCase
             'placeholder' => 'short_object_description_placeholder',
             'link_parameters' => array(),
             'show_filter' => null,
+            'advanced_filter' => true,
         ), $filter->getOptions());
     }
 
@@ -150,6 +144,7 @@ class DatagridMapperTest extends \PHPUnit_Framework_TestCase
             'foo_filter_option' => 'foo_filter_option_value',
             'link_parameters' => array(),
             'show_filter' => null,
+            'advanced_filter' => true,
         ), $filter->getOptions());
     }
 

+ 1 - 1
Tests/Filter/FilterTest.php

@@ -11,7 +11,6 @@
 
 namespace Sonata\AdminBundle\Tests\Filter;
 
-use Sonata\AdminBundle\Filter\Filter;
 use Sonata\AdminBundle\Tests\Fixtures\Filter\FooFilter;
 
 class FilterTest extends \PHPUnit_Framework_TestCase
@@ -39,6 +38,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
         $expected = $options;
         $expected['foo'] = 'bar';
         $expected['show_filter'] = null;
+        $expected['advanced_filter'] = true;
 
         $this->assertEquals($expected, $filter->getOptions());
         $this->assertEquals('name', $filter->getFieldName());