Skip to content

Commit

Permalink
ENH: V3 relese cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Dec 22, 2023
1 parent df20b52 commit 8b22a34
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 124 deletions.
81 changes: 48 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ Overall this module allows you to fully customise your `GridField` filters inclu

## Installation

`composer require silverstripe-terraformers/gridfield-rich-filter-header dev-master`
`composer require silverstripe-terraformers/gridfield-rich-filter-header`

## Basic configuration

Full filter configuration format looks like this:

```
```php
'GridField_column_name' => [
'title' => 'DB_column_name',
'filter' => 'search_filter_type',
Expand All @@ -53,7 +53,7 @@ Full filter configuration format looks like this:

Concrete example:

```
```php
'Expires.Nice' => [
'title' => 'Expires',
'filter' => 'ExactMatchFilter',
Expand All @@ -69,22 +69,26 @@ Shorter configuration formats are available as well:
Field mapping version doesn't include filter specification and will use `PartialMatchFilter`.
This should be used if you are happy with using `PartialMatchFilter`

```
```php
'GridField_column_name' => 'DB_column_name',
```

Whitelist version doesn't include filter specification nor field mapping.
This configuration will use `PartialMatchFilter` and will assume that both `GridField_column_name` and `DB_column_name` are the same.

```
```php
'GridField_column_name',
```

Multiple filters configuration example:

```
$gridFieldConfig->removeComponentsByType(GridFieldFilterHeader::class);
```php
$gridFieldConfig->removeComponentsByType(
GridFieldSortableHeader::class,
GridFieldFilterHeader::class,
);

$sort = new RichSortableHeader();
$filter = new RichFilterHeader();
$filter
->setFilterConfig([
Expand All @@ -96,20 +100,22 @@ $filter
]);


$gridFieldConfig->addComponent($sort, GridFieldPaginator::class);
$gridFieldConfig->addComponent($filter, GridFieldPaginator::class);
```

If no configuration is provided via `setFilterConfig` method, filter configuration will fall back to `searchable_fields` of the `DataObject` that is listed in the `GridField`.
If `searchable_fields` configuration is not available, `summary_fields` will be used instead.

Make sure you add the `RichFilterHeader` component BEFORE the `GridFieldPaginator`
otherwise your paginaton will be broken since you always want to filter before paginating.
otherwise your pagination will be broken since you always want to filter before paginating.
Sort header component also needs to be replaced to allow the filter expand button to be shown.

## Field configuration

Any `FormField` can be used for filtering. You just need to add it to filter configuration like this:

```
```php
->setFilterFields([
'Expires' => DateField::create('', ''),
])
Expand All @@ -125,7 +131,7 @@ If filter method is specified for a field, it will override the standard filter.
Filter method is a callback which will be applied to the `DataList` and you are free to add any functionality you need
inside the callback. Make sure that your callback returns a `DataList` with the same `DataClass` as the original.

```
```php
->setFilterMethods([
'Title' => function (DataList $list, $name, $value) {
// my custom filter logic is implemented here
Expand All @@ -143,7 +149,7 @@ For your convenience there are couple of filter methods available to cover some

Both of these filters can be used in `setFilterMethods` like this:

```
```php
->setFilterMethods([
'Title' => RichFilterHeader::FILTER_ALL_KEYWORDS,
])
Expand All @@ -156,9 +162,13 @@ Both of these filters can be used in `setFilterMethods` like this:
* filter with `AutoCompleteField` and filtering by `AllKeywordsFilter` filter method
* filter with `DateField` and filtering by `StartsWithFilter`

```
$gridFieldConfig->removeComponentsByType(GridFieldFilterHeader::class);
```php
$gridFieldConfig->removeComponentsByType(
GridFieldSortableHeader::class,
GridFieldFilterHeader::class,
);

$sort = new RichSortableHeader();
$filter = new RichFilterHeader();
$filter
->setFilterConfig([
Expand All @@ -185,6 +195,7 @@ $dealsLookup
->setSourceSort('Label ASC')
->setRequireSelection(false);

$gridFieldConfig->addComponent($sort, GridFieldPaginator::class);
$gridFieldConfig->addComponent($filter, GridFieldPaginator::class);
```

Expand All @@ -197,9 +208,13 @@ $gridFieldConfig->addComponent($filter, GridFieldPaginator::class);

Note that the items that are listed in the `GridField` have a `many_many` relation with `TaxonomyTerm` called `TaxonomyTerms`

```
$gridFieldConfig->removeComponentsByType(GridFieldFilterHeader::class);
```php
$gridFieldConfig->removeComponentsByType(
GridFieldSortableHeader::class,
GridFieldFilterHeader::class,
);

$sort = new RichSortableHeader();
$filter = new RichFilterHeader();
$filter
->setFilterConfig([
Expand All @@ -216,6 +231,7 @@ $filter
'TaxonomyTerms' => RichFilterHeader::FILTER_MANY_MANY_RELATION,
]);

$gridFieldConfig->addComponent($sort, GridFieldPaginator::class);
$gridFieldConfig->addComponent($filter, GridFieldPaginator::class);
```

Expand All @@ -227,9 +243,13 @@ $gridFieldConfig->addComponent($filter, GridFieldPaginator::class);

Our custom filter method filters records by three different `DB` columns via `PartialMatch` filter.

```
$gridFieldConfig->removeComponentsByType(GridFieldFilterHeader::class);
```php
$gridFieldConfig->removeComponentsByType(
GridFieldSortableHeader::class,
GridFieldFilterHeader::class,
);

$sort = new RichSortableHeader();
$filter = new RichFilterHeader();
$filter
->setFilterConfig([
Expand All @@ -249,6 +269,7 @@ $filter
]);

$label->setAttribute('placeholder', 'Filter by three different columns');
$gridFieldConfig->addComponent($sort, GridFieldPaginator::class);
$gridFieldConfig->addComponent($filter, GridFieldPaginator::class);
```

Expand All @@ -261,7 +282,7 @@ This example covers
* `Team` (has many `Player`)
* `PlayersAdmin`

```
```php
<?php

namespace App\Models;
Expand All @@ -270,11 +291,8 @@ use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\HasManyList;

/**
* Class Team
*
* @property string $Title
* @method HasManyList|Player[] Players()
* @package App\Models
*/
class Team extends DataObject
{
Expand All @@ -299,20 +317,17 @@ class Team extends DataObject
}
```

```
```php
<?php

namespace App\Models;

use SilverStripe\ORM\DataObject;

/**
* Class Player
*
* @property string $Title
* @property int $TeamID
* @method Team Team()
* @package App\Models
*/
class Player extends DataObject
{
Expand Down Expand Up @@ -345,7 +360,7 @@ class Player extends DataObject
}
```

```
```php
<?php

namespace App\Admin;
Expand All @@ -361,11 +376,6 @@ use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use Terraformers\RichFilterHeader\Form\GridField\RichFilterHeader;

/**
* Class PlayersAdmin
*
* @package App\Admin
*/
class PlayersAdmin extends ModelAdmin
{
/**
Expand Down Expand Up @@ -402,8 +412,12 @@ class PlayersAdmin extends ModelAdmin
$config = $gridField->getConfig();

// custom filters
$config->removeComponentsByType(GridFieldFilterHeader::class);
$config->removeComponentsByType(
GridFieldSortableHeader::class,
GridFieldFilterHeader::class,
);

$sort = new RichSortableHeader();
$filter = new RichFilterHeader();
$filter
->setFilterConfig([
Expand All @@ -423,6 +437,7 @@ class PlayersAdmin extends ModelAdmin


$team->setEmptyString('-- select --');
$config->addComponent($sort, GridFieldPaginator::class);
$config->addComponent($filter, GridFieldPaginator::class);
}

Expand Down
7 changes: 0 additions & 7 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ SilverStripe\Forms\FormRequestHandler:
- Terraformers\RichFilterHeader\Extension\GridFieldRichFilterHeaderRequestExtension
url_handlers:
'field/$FieldName!': 'handleFieldComposite'

# legacy option for filter header component
# we need to use this as new rect version of this component uses a different way to scaffold search fields
# this needs to be removed before SS 5 upgrade and the effort contained means checking every GridField that
# uses this component and validating if the desired fields display works fine
SilverStripe\Forms\GridField\GridFieldFilterHeader:
force_legacy: true
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@
"dev-2": "2.x-dev"
}
},
"scripts": {
"lint": "phpcs src/ tests/php/",
"lint-clean": "phpcbf src/ tests/php/"
},
"autoload": {
"psr-4": {
"Terraformers\\RichFilterHeader\\": "src/",
"Terraformers\\RichFilterHeader\\Tests\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/recipe-plugin": true,
"silverstripe/vendor-plugin": true
}
}
}
32 changes: 32 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>tests</file>

<!-- Show progress and output sniff names on violation, and add colours -->
<arg value="p" />
<arg name="colors" />
<arg value="s" />

<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
<exclude name="PSR2.Classes.PropertyDeclaration" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration" /> <!-- causes php notice while linting -->
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenerdefault" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment" />
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
<exclude name="Squiz.Scope.MethodScope" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
</rule>

<!-- PHP-PEG generated file not intended for human consumption -->
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>
</ruleset>
22 changes: 13 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/cms/tests/bootstrap.php"
colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Default">
<directory>tests/php/</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</testsuites>
</phpunit>
6 changes: 1 addition & 5 deletions src/Form/GridField/RichFilterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use SilverStripe\View\SSViewer;

/**
* Class RichFilterHeader
*
* filter header with customisable filter fields and filters
* fields that use XHR are supported
*
Expand Down Expand Up @@ -139,8 +137,6 @@
* ])
*
* this is a great way to cover edge cases as the implementation of the filter is completely up to the developer
*
* @package Terraformers\RichFilterHeader\Form\GridField
*/
class RichFilterHeader extends GridFieldFilterHeader
{
Expand Down Expand Up @@ -598,7 +594,7 @@ protected function hasFilterField($field)
* @param GridField $gridField
* @return array|null
*/
public function getHTMLFragments($gridField)
public function getHTMLFragments(mixed $gridField): mixed
{
$list = $gridField->getList();
if (!$this->checkDataType($list)) {
Expand Down
Loading

0 comments on commit 8b22a34

Please sign in to comment.