Sortable SS_DateTime.Nice() fields


/**
* Set the formatting of sortable Date fields so they are 'Nice' and still sortable
*
* @param int|null $id
* @param array|null $fields
* @return CMSForm
*/
public function getEditForm($id = null, $fields = null) {
/** @var CMSForm $form */
/** @var GridField $gridField */
$form = parent::getEditForm($id, $fields);
$gridField = $form->Fields()->first();
$config = $gridField->getConfig();
$config->removeComponentsByType('GridFieldDataColumns');

// Need to remove then later re-add Delete + Edit components otherwise they end up
// on the left-hand side of the GridField
$config->removeComponentsByType('GridFieldDeleteAction');
$config->removeComponentsByType('GridFieldEditButton');

// Create a new GridFieldDataColumns with explicit formatting for sortable Date fields
$gridFieldDataColumns = new GridFieldDataColumns();
foreach (array('Created', 'LastEdited') as $key) {
$gridFieldDataColumns->setFieldFormatting(array(
$key => function($value, DataObject $item) {
$date = new SS_Datetime();
$date->setValue($value);
return $date->Nice();
}
));
}
$config->addComponents(
$gridFieldDataColumns,
new GridFieldEditButton(),
new GridFieldDeleteAction()
);
return $form;
}