silverstripe-framework/src/Forms/GridField/GridFieldAddNewButton.php

60 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\Control\Controller;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;
/**
2014-08-15 18:53:05 +12:00
* This component provides a button for opening the add new form provided by
* {@link GridFieldDetailForm}.
*
2014-08-15 18:53:05 +12:00
* Only returns a button if {@link DataObject->canCreate()} for this record
* returns true.
*/
2016-11-29 12:31:16 +13:00
class GridFieldAddNewButton implements GridField_HTMLProvider
{
2016-11-29 12:31:16 +13:00
protected $targetFragment;
2016-11-29 12:31:16 +13:00
protected $buttonName;
2016-11-29 12:31:16 +13:00
public function setButtonName($name)
{
$this->buttonName = $name;
2016-11-29 12:31:16 +13:00
return $this;
}
2016-11-29 12:31:16 +13:00
public function __construct($targetFragment = 'before')
{
$this->targetFragment = $targetFragment;
}
2016-11-29 12:31:16 +13:00
public function getHTMLFragments($gridField)
{
$singleton = singleton($gridField->getModelClass());
2016-11-29 12:31:16 +13:00
if (!$singleton->canCreate()) {
return array();
}
2016-11-29 12:31:16 +13:00
if (!$this->buttonName) {
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
$objectName = $singleton->i18n_singular_name();
2017-04-20 13:15:24 +12:00
$this->buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.Add', 'Add {name}', array('name' => $objectName));
2016-11-29 12:31:16 +13:00
}
2016-11-29 12:31:16 +13:00
$data = new ArrayData(array(
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
'ButtonName' => $this->buttonName,
));
2016-11-29 12:31:16 +13:00
$templates = SSViewer::get_templates_by_class($this, '', __CLASS__);
return array(
$this->targetFragment => $data->renderWith($templates),
);
}
}