Hello,
I'm a total newbie to Silverstripe. I have the following for uploading reports in PDF format using the CMS and then list it by category in the front end for users to download:
File: Report.php
class Report extends DataObject {
private static $db = array(
'Name' => 'Varchar'
);
private static $has_one = array(
'Page' => 'ReportPage',
'Category' => 'ReportCategory',
'Attachment' => 'File'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$attachment = new UploadField('Attachment', $this->Attachment);
$attachment->getValidator()->setAllowedExtensions('pdf');
$attachment->setFolderName('Uploads/Reports');
$fields->addFieldToTab('Root.Main', $attachment);
return $fields;
}
}
File: ReportCategory.php
class ReportCategory extends DataObject {
private static $db = array(
'Name' => 'Varchar'
);
private static $has_one = array(
'Page' => 'ReportPage'
);
private static $has_many = array(
'Reports' => 'Report'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$reportsConfig = GridFieldConfig_RelationEditor::create();
$reportsConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Name' => 'Name',
'Attachment.Name' => 'Attachment',
'Page.Title' => 'Page'
));
$reportsField = new GridField(
'Reports',
'Report',
$this->Reports()->sort('Name', ASC),
$reportsConfig
);
$fields->removeByName('Reports');
if ($this->ID != 0) {
$fields->addFieldToTab('Root.Main', $reportsField);
}
return $fields;
}
}
File: ReportPage.php
class ReportPage extends Page {
private static $has_many = array(
'Categories' => 'ReportCategory',
'Reports' => 'Report'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$categoriesConfig = GridFieldConfig_RelationEditor::create();
$categoriesConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Name' => 'Name'
));
$categoriesField = new GridField(
'Categories',
'Category',
$this->Categories()->sort('Name', DESC),
$categoriesConfig
);
$reportsConfig = GridFieldConfig_RelationEditor::create();
$reportsConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Category.Name' => 'Category',
'Name' => 'Name',
'Attachment.Name' => 'Attachment'
));
$reportsField = new GridField(
'Reports',
'Report',
$this->Reports()->sort('Name', DESC),
$reportsConfig
);
$fields->addFieldToTab('Root.Reports', $categoriesField);
$fields->addFieldToTab('Root.Reports', $reportsField);
return $fields;
}
public function CategoriesList() {
if($this->Categories()->exists()) {
return implode(', ', $this->Categories()->column('Name'));
}
}
}
class ReportPage_Controller extends Page_Controller {
public function init() {
parent::init();
Requirements::javascript($this->ThemeDir()."/javascript/accordion.js");
}
}
So on my Reports page I can enter new Report without going through the Category screen. All works fine and dandy. However on the Report creation/edit screen, Category dropdown, which is platform-generated, is listed in order of creation time. How do I go about changing that to list by alphabetical order?
Many thanks.
Jonathan