Merge pull request #131 from robbieaverill/pulls/2.0/add-belongstoset-column

NEW Add column to document set GridField to show whether added manually or not
This commit is contained in:
sachajudd 2017-05-18 15:42:10 +12:00 committed by GitHub
commit 6afab52a09
2 changed files with 55 additions and 2 deletions

View File

@ -9,7 +9,6 @@
*/ */
class DMSDocumentSet extends DataObject class DMSDocumentSet extends DataObject
{ {
private static $db = array( private static $db = array(
'Title' => 'Varchar(255)', 'Title' => 'Varchar(255)',
'KeyValuePairs' => 'Text', 'KeyValuePairs' => 'Text',
@ -111,11 +110,17 @@ class DMSDocumentSet extends DataObject
} }
$gridFieldConfig->getComponentByType('GridFieldDataColumns') $gridFieldConfig->getComponentByType('GridFieldDataColumns')
->setDisplayFields(DMSDocument::create()->config()->get('display_fields')) ->setDisplayFields($self->getDocumentDisplayFields())
->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) ->setFieldCasting(array('LastEdited' => 'Datetime->Ago'))
->setFieldFormatting( ->setFieldFormatting(
array( array(
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>', 'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>',
'BelongsToSet' => function ($value) {
if ($value) {
return _t('DMSDocumentSet.MANUAL', 'Manually');
}
return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder');
}
) )
); );
@ -260,4 +265,17 @@ class DMSDocumentSet extends DataObject
} }
} }
} }
/**
* Customise the display fields for the documents GridField
*
* @return array
*/
public function getDocumentDisplayFields()
{
return array_merge(
(array) DMSDocument::create()->config()->get('display_fields'),
array('BelongsToSet' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added'))
);
}
} }

View File

@ -84,6 +84,41 @@ class DMSDocumentSetTest extends SapphireTest
$this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows')); $this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows'));
} }
/**
* Ensure that the display fields for the documents GridField can be returned
*/
public function testGetDocumentDisplayFields()
{
$document = $this->objFromFixture('DMSDocumentSet', 'ds1');
$this->assertInternalType('array', $document->getDocumentDisplayFields());
Config::inst()->update('DMSDocument', 'display_fields', array('apple' => 'Apple', 'orange' => 'Orange'));
$displayFields = $document->getDocumentDisplayFields();
$this->assertContains('Apple', $displayFields);
$this->assertContains('Orange', $displayFields);
$this->assertArrayHasKey('BelongsToSet', $displayFields);
$this->assertContains('Added', $displayFields);
}
/**
* Tests to ensure that the callback for formatting BelongsToSet will return a nice label for the user
*/
public function testNiceFormattingForBelongsToSetInGridField()
{
$fieldFormatting = $this->objFromFixture('DMSDocumentSet', 'ds1')
->getCMSFields()
->fieldByName('Root.Main.Documents')
->getConfig()
->getComponentByType('GridFieldDataColumns')
->getFieldFormatting();
$this->assertArrayHasKey('BelongsToSet', $fieldFormatting);
$this->assertTrue(is_callable($fieldFormatting['BelongsToSet']));
$this->assertSame('Manually', $fieldFormatting['BelongsToSet'](1));
$this->assertSame('Query Builder', $fieldFormatting['BelongsToSet'](0));
}
/** /**
* Test that query fields can be added to the gridfield * Test that query fields can be added to the gridfield
*/ */