ENHANCEMENT: SSF-106 adding the ability to turn off and on the "add new" button on the GridFieldTitle

This commit is contained in:
Julian Seidenberg 2012-03-06 11:19:46 +13:00
parent 8284fcc75b
commit a715785a42
4 changed files with 48 additions and 3 deletions

View File

@ -1,13 +1,25 @@
<?php
class GridFieldTitle implements GridField_HTMLProvider {
protected $newEnabled = true;
function getHTMLFragments($gridField) {
return array(
'header' => $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;
}
}
?>

View File

@ -1,3 +1,3 @@
<tr class="title">
<th colspan="$ColumnCount"><h2>$Title</h2> <a href="$NewLink" class="action ss-ui-action-constructive ss-ui-button ui-button ui-widget ui-state-default ui-corner-all new new-link" data-icon="add"><% _t('GridField.AddNew', 'Add New') %></a></th>
<th colspan="$ColumnCount"><h2>$Title</h2><% if NewEnabled %> <a href="$NewLink" class="action ss-ui-action-constructive ss-ui-button ui-button ui-widget ui-state-default ui-corner-all new new-link" data-icon="add"><% _t('GridField.AddNew', 'Add New') %></a><% end_if %></th>
</tr>

View File

@ -1,5 +1,5 @@
<?php
class GridFieldtest extends SapphireTest {
class GridFieldTest extends SapphireTest {
/**
* @covers GridField::__construct

View File

@ -0,0 +1,33 @@
<?php
class GridFieldTitleTest extends SapphireTest {
public function testGridTitleAddNewEnabled() {
//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(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");
}
}
?>