Jump to:

23358 Posts in 17875 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Make a FieldList and a gridfield inside a ModelAdmin

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

Page: 1
Go to End
Author Topic: 695 Views
  • sajok
    Avatar
    Community Member
    82 Posts

    Make a FieldList and a gridfield inside a ModelAdmin Link to this post

    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

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Make a FieldList and a gridfield inside a ModelAdmin Link to this post

    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;

    }

  • sajok
    Avatar
    Community Member
    82 Posts

    Re: Make a FieldList and a gridfield inside a ModelAdmin Link to this post

    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?

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Make a FieldList and a gridfield inside a ModelAdmin Link to this post

    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")) );

    695 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.