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