FIX Don't show GridField until set has been saved. Add more tests, fix typos in docs.

This commit is contained in:
Robbie Averill 2017-05-09 10:13:13 +12:00
parent 66abd22ee5
commit 1c1b1d2aeb
5 changed files with 98 additions and 16 deletions

View File

@ -13,7 +13,7 @@ class DMSGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
//add a data attribute specifying how many pages this document is referenced on
if ($record = $this->record) {
$numberOfPageRelations = $record->getRelatedPages()->Count();
$numberOfPageRelations = $record->getRelatedPages()->count();
$relations = new ShortCodeRelationFinder();
$numberOfInlineRelations = $relations->findPageCount($record->ID);

View File

@ -47,14 +47,25 @@ class DMSDocumentSet extends DataObject
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Javascript to customize the grid field for the DMS document (overriding entwine
// in FRAMEWORK_DIR.'/javascript/GridField.js'
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js');
Requirements::css(DMS_DIR . '/css/DMSMainCMS.css');
// PHP 5.3 only
$self = $this;
// Javascript for the link editor pop-up in TinyMCE
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js');
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) {
// Don't put the GridField for documents in until the set has been created
if (!$self->isInDB()) {
$fields->addFieldToTab(
'Root.Main',
LiteralField::create(
'GridFieldNotice',
'<p class="message warning">' . _t(
'DMSDocumentSet.GRIDFIELD_NOTICE',
'Managing documents will be available once you have created this document set.'
) . '</p>'
),
'Title'
);
return;
}
// Document listing
$gridFieldConfig = GridFieldConfig::create()
@ -65,9 +76,9 @@ class DMSDocumentSet extends DataObject
// new GridFieldOrderableRows('DocumentSort'),
new GridFieldDataColumns(),
new GridFieldEditButton(),
new DMSGridFieldDeleteAction(), //special delete dialog to handle custom behaviour of unlinking and deleting
// Special delete dialog to handle custom behaviour of unlinking and deleting
new DMSGridFieldDeleteAction(),
new GridFieldDetailForm()
//GridFieldLevelup::create($folder->ID)->setLinkSpec('admin/assets/show/%d')
);
if (class_exists('GridFieldPaginatorWithShowAll')) {
@ -79,7 +90,7 @@ class DMSDocumentSet extends DataObject
if (class_exists('GridFieldSortableRows')) {
$sortableComponent = new GridFieldSortableRows('DocumentSort');
//setUsePagenation method removed from newer version of SortableGridField.
// setUsePagenation method removed from newer version of SortableGridField.
if (method_exists($sortableComponent, 'setUsePagination')) {
$sortableComponent->setUsePagination(false)->setForceRedraw(true);
}
@ -90,21 +101,21 @@ class DMSDocumentSet extends DataObject
singleton('DMSDocument');
$gridFieldConfig->getComponentByType('GridFieldDataColumns')
->setDisplayFields(Config::inst()->get('DMSDocument', 'display_fields'))
->setFieldCasting(array('LastChanged'=>"Datetime->Ago"))
->setFieldCasting(array('LastChanged' => 'Datetime->Ago'))
->setFieldFormatting(
array(
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>'
)
);
//override delete functionality with this class
// Override delete functionality with this class
$gridFieldConfig->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest');
$gridField = GridField::create(
'Documents',
false,
$this->Documents(),//->Sort('DocumentSort'),
$self->Documents(), //->Sort('DocumentSort'),
$gridFieldConfig
);
$gridField->addExtraClass('documents');
@ -113,11 +124,30 @@ class DMSDocumentSet extends DataObject
$addNewButton = new DMSGridFieldAddNewButton,
'GridFieldExportButton'
);
$addNewButton->setDocumentSetId($this->ID);
$addNewButton->setDocumentSetId($self->ID);
$fields->removeByName('Documents');
$fields->addFieldToTab('Root.Main', $gridField);
});
$this->addRequirements();
return parent::getCMSFields();
}
/**
* Add required CSS and Javascript requirements for managing documents
*
* @return $this
*/
protected function addRequirements()
{
// Javascript to customize the grid field for the DMS document (overriding entwine
// in FRAMEWORK_DIR.'/javascript/GridField.js'
Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js');
Requirements::css(DMS_DIR . '/css/DMSMainCMS.css');
// Javascript for the link editor pop-up in TinyMCE
Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js');
return $this;
}
}

View File

@ -5,7 +5,7 @@
Documents now belong to "sets", which are attached to Pages. A Page can have many Document Sets, and a Set has a
many_many relationship with Documents.
When upgrading from 1.x to 2.x you will need to migrate the relationships from your Pages to Documents so support
When upgrading from 1.x to 2.x you will need to migrate the relationships from your Pages to Documents to support
having a Document Set intermediary (@todo Add a build task for this).
## API changes

View File

@ -21,6 +21,7 @@ en:
DMSDocumentSet:
ADDDOCUMENTBUTTON: Add Document
ADDDOCUMENTSBUTTON: Add Documents
GRIDFIELD_NOTICE: Managing documents will be available once you have created this document set.
PLURALNAME: Document Sets
SINGULARNAME: Document Set
DMSTag:

View File

@ -18,6 +18,28 @@ class DMSDocumentSetTest extends SapphireTest
$this->assertSame('Extended', $documents->first()->Filename);
}
/**
* Test that the GridField for documents isn't shown until you've saved the set
*/
public function testGridFieldShowsWhenSetIsSaved()
{
$set = DMSDocumentSet::create();
// Not in database yet
$fields = $set->getCMSFields();
$this->assertNull($fields->fieldByName('Root.Main.Documents'));
$gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice');
$this->assertNotNull($gridFieldNotice);
$this->assertContains('Managing documents will be available', $gridFieldNotice->getContent());
// In the database
$set->Title = 'Testing';
$set->write();
$fields = $set->getCMSFields();
$this->assertNotNull($fields->fieldByName('Root.Main.Documents'));
$this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice'));
}
public function testRelations()
{
$s1 = $this->objFromFixture('SiteTree', 's1');
@ -32,4 +54,33 @@ class DMSDocumentSetTest extends SapphireTest
$this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets');
$this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID'));
}
/**
* Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context.
*/
public function testDocumentGridFieldConfig()
{
$set = $this->objFromFixture('DMSDocumentSet', 'ds1');
$fields = $set->getCMSFields();
$gridField = $fields->fieldByName('Root.Main.Documents');
$this->assertTrue((bool) $gridField->hasClass('documents'));
/** @var GridFieldConfig $config */
$config = $gridField->getConfig();
$this->assertNotNull($config->getComponentByType('DMSGridFieldDeleteAction'));
$this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton'));
$this->assertSame($set->ID, $addNew->getDocumentSetId());
if (class_exists('GridFieldPaginatorWithShowAll')) {
$this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll'));
} else {
$paginator = $config->getComponentByType('GridFieldPaginator');
$this->assertNotNull($paginator);
$this->assertSame(15, $paginator->getItemsPerPage());
}
$sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull';
$this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows'));
}
}