Skip to content

Commit

Permalink
Add possibility to hide advanced filters
Browse files Browse the repository at this point in the history
  • Loading branch information
qsomazzi committed Apr 14, 2015
1 parent 4ab4a8d commit 4912fbc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function getAssociationMapping()
public function setOptions(array $options)
{
$this->options = array_merge(
array('show_filter' => null),
array('show_filter' => null, 'advanced_filter' => true),
$this->getDefaultOptions(),
$options
);
Expand Down
8 changes: 6 additions & 2 deletions Form/Type/Filter/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
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']))
;
}
Expand All @@ -67,7 +69,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'field_type' => 'choice',
'field_options' => array()
'field_options' => array(),
'operator_type' => 'choice',
'operator_options' => [],
));
}
}
11 changes: 11 additions & 0 deletions Resources/doc/reference/action_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^
Expand Down
19 changes: 13 additions & 6 deletions Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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) %}
Expand All @@ -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">
Expand All @@ -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>

Expand Down
6 changes: 3 additions & 3 deletions Tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function testGetBaseRouteNameWithChildAdmin()
}

/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testGetBaseRouteNameWithUnreconizedClassname()
{
Expand Down Expand Up @@ -672,7 +672,7 @@ public function testSubClass()
}

/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testNonExistantSubclass()
{
Expand Down Expand Up @@ -1529,7 +1529,7 @@ public function testGetFilterFieldDescription()
break;

default:
throw new \RuntiemException(sprintf('Unknown filter name "%s"', $name));
throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
break;
}

Expand Down
9 changes: 2 additions & 7 deletions Tests/Datagrid/DatagridMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -122,6 +115,7 @@ public function testGet()
'placeholder' => 'short_object_description_placeholder',
'link_parameters' => array(),
'show_filter' => null,
'advanced_filter' => true,
), $filter->getOptions());
}

Expand Down Expand Up @@ -150,6 +144,7 @@ public function testGet2()
'foo_filter_option' => 'foo_filter_option_value',
'link_parameters' => array(),
'show_filter' => null,
'advanced_filter' => true,
), $filter->getOptions());
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Filter/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +38,7 @@ public function testFilter()
$expected = $options;
$expected['foo'] = 'bar';
$expected['show_filter'] = null;
$expected['advanced_filter'] = true;

$this->assertEquals($expected, $filter->getOptions());
$this->assertEquals('name', $filter->getFieldName());
Expand Down

0 comments on commit 4912fbc

Please sign in to comment.