2

I know there is a GridFieldExportButton which exports all the data of a GridField. But what I want is a custom Button which exports all the $db fields (ore just a few of them) of only ONE DataObject in a CSV file and download it. So I want this button in the edit area of this one DataObject and not for the GridField which shows all data objects.

I have already the button, now I need the right function. Can somebody help me?

Barry
  • 3,303
  • 7
  • 23
  • 42
iraira
  • 315
  • 2
  • 13
  • Maybe this would help you to formulate a solution? https://github.com/silverstripe/silverstripe-userforms/blob/master/code/model/submissions/SubmittedForm.php . On the latest version of the user defined form adds a export button when you view the submissions to export only that Dataobjects data. – Olli Tyynelä Jan 14 '16 at 15:42
  • Thanks for answering! But unfortunately this don't really work for me because I don't have a dataobject with a has_many relation to the fields I want to export. I want just to export the $db fields of the dataobject. Do you have an idea how to do that? – iraira Jan 18 '16 at 10:31
  • Yes I noted it exports relations but if it can export complex things presumably there is a way to mod it to export non relational things also :). Sadly I don't have the time to make a working example at the time. – Olli Tyynelä Jan 18 '16 at 11:40

1 Answers1

2

You can change the fields exported for any DataObject in ModelAdmin with the following:

ModelAdmin:

class MyModelAdmin extends ModelAdmin {

    ...

    static $managed_models = array(
        'MyDataObject'
    );

    ...

    public function getExportFields() {
        $modelClass = singleton($this->modelClass);

        return $modelClass->hasMethod('getExportFields')
            ? $modelClass->getExportFields()
            : $modelClass->summaryFields();
    }

    ...
}

MyDataObject:

class MyDataObject extends DataObject {

    ...

    public function getExportFields() {
        $exportFields = array(
            //Add all "db" fields here
        );
        return $exportFields;
    }

    ...
}

If you wish for the export to be attached to a button else where I would suggest that you change the button to link to the ModelAdmin export CSV link

Barry
  • 3,303
  • 7
  • 23
  • 42