Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Sorting has_one Dropdown menu


Go to End


2 Posts   1322 Views

Avatar
Jon Yeo

Community Member, 1 Post

10 October 2015 at 9:45am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

24 October 2015 at 9:43am

You could remove the scaffolded dropdown and replace it with your own version in getCMSFields(). Off the top of my head:

public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->removeByName('ReportCategory');
        $fields->push(
                DropdownField::create('ReportCategoryID', 'Category', ReportCategory::get()->sort('Name')->map('ID', 'Name'))
        );
        ...