public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false)
{
parent::__construct();
$this->blockManager = Injector::inst()->get('BlockManager');
$controllerClass = Controller::curr()->class;
// Get available Areas (for page) or all in case of ModelAdmin
if ($controllerClass == 'CMSPageEditController') {
$currentPage = Controller::curr()->currentPage();
$areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName);
} else {
$areasFieldSource = $this->blockManager->getAreasForTheme();
}
// EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved
if ($editableRows) {
$this->addComponent($editable = new GridFieldEditableColumns());
$displayfields = array('TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'), 'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'ReadonlyField'), 'BlockArea' => array('title' => _t('Block.BlockArea', 'Block Area') . '
', 'callback' => function () use($areasFieldSource) {
$areasField = DropdownField::create('BlockArea', 'Block Area', $areasFieldSource);
if (count($areasFieldSource) > 1) {
$areasField->setHasEmptyDefault(true);
}
return $areasField;
}), 'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'), 'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'LiteralField'));
if ($aboveOrBelow) {
$displayfields['AboveOrBelow'] = array('title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), 'callback' => function () {
return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options'));
});
}
$editable->setDisplayFields($displayfields);
} else {
$this->addComponent($dcols = new GridFieldDataColumns());
$displayfields = array('TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'), 'Title' => _t('Block.Title', 'Title'), 'BlockArea' => _t('Block.BlockArea', 'Block Area'), 'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'), 'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'));
$dcols->setDisplayFields($displayfields);
$dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw'));
}
$this->addComponent(new GridFieldButtonRow('before'));
$this->addComponent(new GridFieldToolbarHeader());
$this->addComponent(new GridFieldDetailForm());
$this->addComponent($sort = new GridFieldSortableHeader());
$this->addComponent($filter = new GridFieldFilterHeader());
$this->addComponent(new GridFieldDetailForm());
if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) {
$this->addComponent(new GridFieldCopyButton());
}
$filter->setThrowExceptionOnBadDataType(false);
$sort->setThrowExceptionOnBadDataType(false);
if ($canAdd) {
$multiClass = new GridFieldAddNewMultiClass();
$classes = $this->blockManager->getBlockClasses();
$multiClass->setClasses($classes);
$this->addComponent($multiClass);
//$this->addComponent(new GridFieldAddNewButton());
}
if ($canEdit) {
$this->addComponent(new GridFieldEditButton());
}
if ($canDelete) {
$this->addComponent(new GridFieldDeleteAction(true));
}
return $this;
}