silverstripe-blog/src/Admin/GridFieldCategorisationConfig.php

71 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2015-04-21 06:47:46 +02:00
<?php
namespace SilverStripe\Blog\Admin;
use SilverStripe\Blog\Forms\GridField\GridFieldAddByDBField;
use SilverStripe\Blog\Model\CategorisationObject;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
2018-01-29 04:13:19 +01:00
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\ORM\SS_List;
2015-11-21 07:17:29 +01:00
class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor
{
/**
* @param int $itemsPerPage
* @param array|SS_List $mergeRecords
* @param string $parentType
* @param string $parentMethod
* @param string $childMethod
*/
public function __construct($itemsPerPage, $mergeRecords, $parentType, $parentMethod, $childMethod)
2015-11-21 07:17:29 +01:00
{
parent::__construct($itemsPerPage);
$this->removeComponentsByType(GridFieldAddNewButton::class);
2015-11-21 07:17:29 +01:00
$this->addComponent(
GridFieldAddByDBField::create('buttons-before-left')
2015-11-21 07:17:29 +01:00
);
$this->addComponent(
GridFieldMergeAction::create($mergeRecords, $parentType, $parentMethod, $childMethod)
2015-11-21 07:17:29 +01:00
);
/**
* @var GridFieldDataColumns $columns
*/
$columns = $this->getComponentByType(GridFieldDataColumns::class);
2015-11-21 07:17:29 +01:00
$columns->setFieldFormatting(
2017-09-14 00:59:01 +02:00
[
'BlogPostsCount' => function ($value, CategorisationObject $item) {
return $item->BlogPosts()->Count();
}
2017-09-14 00:59:01 +02:00
]
);
2015-11-21 07:17:29 +01:00
$this->changeColumnOrder();
}
/**
* Reorders GridField columns so that Actions is last.
*/
protected function changeColumnOrder()
{
/**
* @var GridFieldDataColumns $columns
*/
$columns = $this->getComponentByType(GridFieldDataColumns::class);
2015-11-21 07:17:29 +01:00
$columns->setDisplayFields(
2017-09-14 00:59:01 +02:00
[
'Title' => _t(__CLASS__ . '.Title', 'Title'),
'BlogPostsCount' => _t(__CLASS__ . '.Posts', 'Posts'),
'MergeAction' => 'MergeAction',
'Actions' => 'Actions'
2017-09-14 00:59:01 +02:00
]
);
2015-11-21 07:17:29 +01:00
}
}