From a715785a4280c2c9c72192918883a735792f6f70 Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Tue, 6 Mar 2012 11:19:46 +1300 Subject: [PATCH] ENHANCEMENT: SSF-106 adding the ability to turn off and on the "add new" button on the GridFieldTitle --- forms/gridfield/GridFieldTitle.php | 14 ++++++++- templates/Includes/GridFieldTitle.ss | 2 +- tests/forms/GridFieldTest.php | 2 +- tests/forms/gridfield/GridFieldTitleTest.php | 33 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/forms/gridfield/GridFieldTitleTest.php diff --git a/forms/gridfield/GridFieldTitle.php b/forms/gridfield/GridFieldTitle.php index afb4480d6..df4b2ed4f 100644 --- a/forms/gridfield/GridFieldTitle.php +++ b/forms/gridfield/GridFieldTitle.php @@ -1,13 +1,25 @@ $gridField->customise(array( - 'NewLink' => Controller::join_links($gridField->Link('item'), 'new') + 'NewLink' => Controller::join_links($gridField->Link('item'), 'new'), + 'NewEnabled' => $this->getNewEnabled() ))->renderWith('GridFieldTitle') ); } + + function getNewEnabled() { + return $this->newEnabled; + } + + function setNewEnabled($enabled) { + $this->newEnabled = $enabled; + } } ?> \ No newline at end of file diff --git a/templates/Includes/GridFieldTitle.ss b/templates/Includes/GridFieldTitle.ss index ef23c0dcf..81f6b4817 100644 --- a/templates/Includes/GridFieldTitle.ss +++ b/templates/Includes/GridFieldTitle.ss @@ -1,3 +1,3 @@ -

$Title

<% _t('GridField.AddNew', 'Add New') %> +

$Title

<% if NewEnabled %> <% _t('GridField.AddNew', 'Add New') %><% end_if %> \ No newline at end of file diff --git a/tests/forms/GridFieldTest.php b/tests/forms/GridFieldTest.php index 0e877eddd..e536628f0 100644 --- a/tests/forms/GridFieldTest.php +++ b/tests/forms/GridFieldTest.php @@ -1,5 +1,5 @@ addComponent($titleField = new GridFieldTitle()); + $actions = new FieldList(); + $grid = new GridField('TestField', 'Test Field', new DataList('Company'),$config); + $fields = new FieldList($rootTab = new TabSet("Root",$tabMain = new Tab('Main',$grid))); + $form = new Form(Controller::curr(), "TestForm", $fields, $actions); + + $titleField->setNewEnabled(true); + $html = $form->forTemplate(); + $this->assertContains('data-icon="add"', $html,"HTML contains the 'add new' button"); + } + + public function testGridTitleAddNewDisabled() { + //construct a fake form field to render out the grid field within it + $config = new GridFieldConfig(); + $config->addComponent($titleField = new GridFieldTitle()); + $actions = new FieldList(); + $grid = new GridField('TestField', 'Test Field', new DataList('Company'),$config); + $fields = new FieldList($rootTab = new TabSet("Root",$tabMain = new Tab('Main',$grid))); + $form = new Form(Controller::curr(), "TestForm", $fields, $actions); + + $titleField->setNewEnabled(false); + $html = $form->forTemplate(); + $this->assertNotContains('data-icon="add"', $html,"HTML does not contain the 'add new' button"); + } +} +?> \ No newline at end of file