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.

General Questions /

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

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

Make a FieldList and a gridfield inside a ModelAdmin


Reply


4 Posts   879 Views

Avatar
sajok

Community Member, 82 Posts

12 May 2013 at 2:52pm

Edited: 12/05/2013 2:57pm

Hello,

I have "Class" and "Student" as two dataobjects that are managed in ModelAdmins. When I click to edit a Class record, two tabs show "Main", and "Students". I can edit Class details in the Main tab, and a list of students that belong to the class show in Students tab. I'm using the following code:

public function getCMSFields() {
// return new FieldList(
// new TextField('Title', 'Class Name'),
// new ReadonlyField('RegisteredStudents', 'Registered Students'),
// new DropdownField('MemberID', 'School Admin', DataList::create("Member")->map("ID", "Title"))
// );

// Get the fields from the parent implementation
$fields = parent::getCMSFields();
// Create a default configuration for the new GridField, allowing record editing
$config = GridFieldConfig_RelationEditor::create();
// Set the names and data for our gridfield columns
$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'FirstName' => 'FirstName',
'Surname' => 'Surname',
'Course.Title'=> 'Course' // Retrieve from a has-one relationship
));
// Create a gridfield to hold the student relationship
$studentsField = new GridField(
'Students', // Field name
'Student', // Field title
$this->Students(), // List of all related students
$config
);
$fields->addFieldToTab('Root.Students', $studentsField);
return $fields;

}

The problem with this is when I uncomment the FieldList at the top of the code so I can override "Class" dataobject fields, the students tab disappears.

any help what's wrong with the code above?
thanks

Avatar
Bambii7

Community Member, 254 Posts

13 May 2013 at 11:58am

You're only returning the class field. You need to capture the parent fields at the top of your function then appeaned new ones before returning.

Try this out

public function getCMSFields() {

// get parent fields
$fields = parent::getCMSFields();

// add class field to form
$classnameField = new FieldList(
   new TextField('Title', 'Class Name'),
   new ReadonlyField('RegisteredStudents', 'Registered Students'),
   new DropdownField('MemberID', 'School Admin', DataList::create("Member")->map("ID", "Title"))
);
$fields->addFieldToTab('Root.Classes', $classnameField);

// Create a default configuration for the new GridField, allowing record editing
$config = GridFieldConfig_RelationEditor::create();
// Set the names and data for our gridfield columns
$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'FirstName' => 'FirstName',
'Surname' => 'Surname',
'Course.Title'=> 'Course' // Retrieve from a has-one relationship
));
// Create a gridfield to hold the student relationship
$studentsField = new GridField(
'Students', // Field name
'Student', // Field title
$this->Students(), // List of all related students
$config
);
$fields->addFieldToTab('Root.Students', $studentsField);

// return my new fields
return $fields;

}

Avatar
sajok

Community Member, 82 Posts

13 May 2013 at 1:21pm

Edited: 13/05/2013 1:22pm

Hi Bambii7,

thanks for replying.. I tried that code, but it shows this error:

[User Error] Uncaught Exception: Object->__call(): the method 'getname' does not exist on 'FieldList'

any idea?

Avatar
Bambii7

Community Member, 254 Posts

15 May 2013 at 11:40am

Oh perhaps take out the field list.

Change

$classnameField = new FieldList(
new TextField('Title', 'Class Name'),
new ReadonlyField('RegisteredStudents', 'Registered Students'),
new DropdownField('MemberID', 'School Admin', DataList::create("Member")->map("ID", "Title"))
);
$fields->addFieldToTab('Root.Classes', $classnameField);


TO

$fields->addFieldToTab( 'Root.Classes', new TextField('Title', 'Class Name') );
$fields->addFieldToTab( 'Root.Classes', new ReadonlyField('RegisteredStudents', 'Registered Students') );
$fields->addFieldToTab( 'Root.Classes', new DropdownField('MemberID', 'School Admin', DataList::create("Member")->map("ID", "Title")) );