2
votes

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?

1
Maybe this would help you to formulate a solution? github.com/silverstripe/silverstripe-userforms/blob/master/code/… . 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ä
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
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ä

1 Answers

2
votes

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