mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
c9b6e9bac0
API Update behaviour of form fields to use standard template lookup mechanism API Support custom "type" parameter to template lookup
53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* This component provides a button for opening the add new form provided by
|
|
* {@link GridFieldDetailForm}.
|
|
*
|
|
* Only returns a button if {@link DataObject->canCreate()} for this record
|
|
* returns true.
|
|
*
|
|
* @package forms
|
|
* @subpackage fields-gridfield
|
|
*/
|
|
class GridFieldAddNewButton implements GridField_HTMLProvider {
|
|
|
|
protected $targetFragment;
|
|
|
|
protected $buttonName;
|
|
|
|
public function setButtonName($name) {
|
|
$this->buttonName = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function __construct($targetFragment = 'before') {
|
|
$this->targetFragment = $targetFragment;
|
|
}
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
$singleton = singleton($gridField->getModelClass());
|
|
|
|
if(!$singleton->canCreate()) {
|
|
return array();
|
|
}
|
|
|
|
if(!$this->buttonName) {
|
|
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
|
|
$objectName = $singleton->i18n_singular_name();
|
|
$this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName));
|
|
}
|
|
|
|
$data = new ArrayData(array(
|
|
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
|
|
'ButtonName' => $this->buttonName,
|
|
));
|
|
|
|
$templates = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
|
return array(
|
|
$this->targetFragment => $data->renderWith($templates),
|
|
);
|
|
}
|
|
|
|
}
|