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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

ListboxField not appearing in CMS Fields


Go to End


2 Posts   936 Views

Avatar
Neomang

Community Member, 9 Posts

7 December 2016 at 8:39am

Edited: 07/12/2016 8:40am

I have a 2 part question. I've got a ListboxField that I've added to my getCMSFields() function but doesn't show up on the page. I confirmed that it's getting the correct data and is actually creating a ListboxField object, but that field just never appears in the CMS. Another ListboxField built the same way shows up fine.

Related to that somewhat, both of the belongs_many_many relations I am using to populate those listbox fields also have their own tab branching off of root.main. However, I never wrote anything that should have built them and they display nothing, how do I get rid of them?

The relevant code is as follows:

class EducationPost extends DataObject{

	private static $db = array(
		'Title' => 'Text',
		'Slug' => 'Text',
		'Teaser' => 'Text',
		'OnFrontPage' => 'Boolean',
		'Published' => 'Boolean'
	);
	
	private static $belongs_many_many = array(
		'Topics' => 'Topic',
		'Resources' => 'Resource'
	);

	public function getCMSFields() {

		$fields = parent::getCMSFields();
		
		Requirements::javascript('mysite/code/title-to-slug.js');

		$fields->addFieldsToTab('Root.Main', array(
			TextField::create('Title', 'Page Title'),
			TextField::create('Slug', 'URL Segment'),
			TextareaField::create('Teaser', 'Teaser description (200 Characters)'),
			CheckboxField::create('OnFrontPage', 'Show on Front Page?'),
			CheckboxField::create('Published', 'Published?')
		));
		
		$source = Topic::get()->map('ID', 'Title')->toArray();
		$value = array_keys($this->Topics()->getIDList());
		$fields->addFieldToTab('Root.Main',
		ListboxField::create('Topics', 'Topic', $source, $value, $size = null, $multiple = true)
		);
		$source = Resource::get()->map('ID', 'Title')->toArray();
		$value = array_keys($this->Resources()->getIDList());
		$fields->addFieldToTab('Root.Main',
		ListboxField::create('Resources', 'Resource', $source, $value, $size = null, $multiple = true)
		, 'Topic');
		
		return $fields;
	}
}

class Resource extends DataObject {
//class Topic is built the same way
    private static $db = array(
		'Title' => 'Varchar',
		'Teaser' => 'Text'
	);

    private static $many_many = array(
		'EducationPosts' => 'EducationPost'
	);
	 
    public function requireDefaultRecords() {
        parent::requireDefaultRecords();
 
        $topics = Topic::get();
        if (!$topics->exists()) {
            $topic = new Topic();
            $topic->Title = 'foo';
            $topic->write();
        }
    }
}

Avatar
martimiz

Forum Moderator, 1391 Posts

9 December 2016 at 2:49am

just a hunch - have you tried changing the name for the offending listbox/class? As resource is a special variable in PHP, that might have something to do with it.

I think ModelAdmin automatically creates a tab for Relations. They should contain a GridField though... You should be able to use RemoveByName in getCMSFields to get rid of them. But I'm not sure why both would contain nothing...