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.

Customising the CMS /

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

How do I configure the datagrid of a Dataobject?


Go to End


4 Posts   930 Views

Avatar
mi32dogs

Community Member, 75 Posts

23 March 2015 at 11:12am

Hi, I’m new to Silverstripe and try to learn it, so bear with me if I ask dumb questions.

I have 2 questions and they are almost the same
a. How do I configure the datagrid of a Dataobject? the datagrid is automatically created but how do I add let’s say a title to the header of the datagrid.
b. I have a dataobject with a $has_many releation to another dataObject, again it automatically creates a tab with a datagrid so I do not have control of the tab or datagrid.

So for both is there a way to configure how the datagrid/tab are created or away to suppress the automatic creation and create them myself?

Avatar
Pyromanik

Community Member, 419 Posts

25 March 2015 at 11:15am

Edited: 25/03/2015 11:18am

public function getCMSFields
Fetch the fields as is with $fields = parent::getCMSFields();
Modify a field by using methods such as $fields->getDataFieldByName()

This way you can modify (and create/add) fields in any way.

http://docs.silverstripe.org/en/tutorials/ is very helpful for beginners.
Although the warning at the top of the page says that the text tutorials are deprecated, pay no attention. They are still very good and very much worth reading. The links are in the sub menu on the left (not in the main text where the video links are).
#2 will be of particular use to you here - http://docs.silverstripe.org/en/tutorials/extending_a_basic_site/

Also relevant:
http://doc.silverstripe.org/en/developer_guides/forms/tabbed_forms/
http://doc.silverstripe.org/en/developer_guides/forms/field_types/gridfield/

More in depth:
http://api.silverstripe.org/3.1/class-DataObject.html#_getCMSFields
http://api.silverstripe.org/3.1/class-FieldList.html
http://api.silverstripe.org/3.1/class-GridField.html
http://api.silverstripe.org/3.1/class-GridFieldConfig.html

Avatar
mi32dogs

Community Member, 75 Posts

26 March 2015 at 5:53am

Hi Pyromanik, thank you for your reply.

This is my getCMSFields() function

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', DateField::create('Date','Date of Press Release')
      ->setConfig('showcalendar', true));
    $fields->addFieldToTab('Root.Main', TextField::create('Headline','Headline'));
    $fields->addFieldToTab('Root.Main', HtmlEditorField::create('Excerpt')->setDescription('Add something like the first paragraph of the press release'));
    $fields->addFieldToTab('Root.Main', $PressReleases = UploadField::create('PressRelease','PressRelease file (PDF only)'));

    $PressReleases
      ->setFolderName('ShowPro/press/PressRelease')
      ->setOverwriteWarning(false)
      ->getValidator()->setAllowedExtensions(array('pdf'));
      $PressReleases->getUpload()->setReplaceFile(false);
    return $fields;
  }

I also set the $summary_fields and the $default_sort to set the columns and order

Now I like to let’s say set the GridFieldPaginator items per page to 5 how do I go about that?
I do not have access to the GridField configuration as it gets created automatically

Avatar
mi32dogs

Community Member, 75 Posts

2 April 2015 at 4:55pm

Ok, I found the answer, for anybody that has the same question, you can do this within your modalAdmin class just add the following:

 public function getEditForm($id = null, $fields = null) {
    $form = parent::getEditForm($id, $fields);

    //This check is simply to ensure you are on the managed model you want adjust accordingly
    if($this->modelClass=='YourClass' && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
      //This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have
      if($gridField instanceof GridField) {
        //$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));
        $gridField->setTitle('YourTitle');
        $gridField->getConfig()->addComponent(new GridFieldOrderableRows('SortOrder'));
        $gridField->getConfig()->getComponentByType('GridFieldPaginator')->setItemsPerPage(20);
      }
    }
    return $form;
  }

Hope that this helps sombody