Allow filtering if a relation is defined or a formatting

This commit is contained in:
Thomas Portelange 2016-10-03 10:23:58 +02:00 committed by Daniel Hensby
parent 1b8e0691c3
commit efbf14be63
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 59 additions and 6 deletions

View File

@ -102,6 +102,29 @@ class GridFieldFilterHeader implements GridField_HTMLProvider, GridField_DataMan
return $dataListClone; return $dataListClone;
} }
/**
* @param string $class
* @param string $column
* @return string
*/
protected function columnToFilterField($class, $column)
{
if (strpos($column, '.') === false) {
return $column;
}
/** @var DataObject $model */
$model = singleton($class);
$columnParts = explode('.', $column);
if ($model->getRelationClass($columnParts[0])) {
return $column;
}
return $columnParts[0];
}
/**
* @param GridField $gridField
* @return array
*/
public function getHTMLFragments($gridField) { public function getHTMLFragments($gridField) {
if(!$this->checkDataType($gridField->getList())) return; if(!$this->checkDataType($gridField->getList())) return;
@ -110,18 +133,21 @@ class GridFieldFilterHeader implements GridField_HTMLProvider, GridField_DataMan
$columns = $gridField->getColumns(); $columns = $gridField->getColumns();
$filterArguments = $gridField->State->GridFieldFilterHeader->Columns->toArray(); $filterArguments = $gridField->State->GridFieldFilterHeader->Columns->toArray();
$currentColumn = 0; $currentColumn = 0;
foreach($columns as $columnField) { foreach($columns as $columnField) {
$currentColumn++; ++$currentColumn;
$metadata = $gridField->getColumnMetadata($columnField); $metadata = $gridField->getColumnMetadata($columnField);
$title = $metadata['title']; $title = $metadata['title'];
$fields = new FieldGroup(); $fields = new FieldGroup();
if($title && $gridField->getList()->canFilterBy($columnField)) { $filterField = $this->columnToFilterField($gridField->getModelClass(), $columnField);
if($title && $gridField->getList()->canFilterBy($filterField)) {
$value = ''; $value = '';
if(isset($filterArguments[$columnField])) { if(isset($filterArguments[$filterField])) {
$value = $filterArguments[$columnField]; $value = $filterArguments[$filterField];
} }
$field = new TextField('filter[' . $gridField->getName() . '][' . $columnField . ']', '', $value); $field = new TextField('filter[' . $gridField->getName() . '][' . $filterField . ']', '', $value);
$field->addExtraClass('ss-gridfield-sort'); $field->addExtraClass('ss-gridfield-sort');
$field->addExtraClass('no-change-track'); $field->addExtraClass('no-change-track');
@ -133,7 +159,7 @@ class GridFieldFilterHeader implements GridField_HTMLProvider, GridField_DataMan
GridField_FormAction::create($gridField, 'reset', false, 'reset', null) GridField_FormAction::create($gridField, 'reset', false, 'reset', null)
->addExtraClass('ss-gridfield-button-reset') ->addExtraClass('ss-gridfield-button-reset')
->setAttribute('title', _t('GridField.ResetFilter', "Reset")) ->setAttribute('title', _t('GridField.ResetFilter', "Reset"))
->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField) ->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $filterField)
); );
} }

View File

@ -0,0 +1,27 @@
<?php
class GridFieldFilterHeaderTest extends SapphireTest {
protected $extraDataObjects = array(
'GridFieldFilterHeaderTest_DataObject',
);
public function testColumnToFilterField() {
$class = 'GridFieldFilterHeaderTest_DataObject';
$header = new GridFieldFilterHeader();
$method = new ReflectionMethod($header, 'columnToFilterField');
$method->setAccessible(true);
$this->assertEquals('Title', $method->invoke($header, $class,'Title.ATT'));
$this->assertEquals('isTest', $method->invoke($header, $class, 'isTest.Nice'));
}
}
class GridFieldFilterHeaderTest_DataObject extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar',
'isTest' => 'Boolean',
);
}