GridfieldImportButton - Many_Many

Silverstripe Version: 4.4.4

Question:
So I have a dataobject (class A) with a many_many relationship (class B). I need my client to be able to create a (class A) dataobject and save it. Then using the GridfieldImportButton be able to upload a csv file that can be processed using a custom CsvBulkLoader to populate the many_many (class B) relations.

I have a CsvBulkLoader that works via ModelAdmin, but cannot get the GridfieldImportButton to work when its added to a relationship Gridfield. I have tried using ->setImportForm and also ->setImportIFrame too.

setImportForm - I tried two ways - a standard form and also a form based of the MemberImportForm as that is used by GridfieldImportButton in the Member modeladmin. Neither worked

setImportIFrame - I created a controller with form and submit function. The form appears in the modal, but on submission the submit function is never called. Code shown below:

<?php

namespace mspacemedia\sh\Controller;

use mspacemedia\sh\BulkLoaders\SHConstituentCsvBulkLoader;
use mspacemedia\sh\SH_Constituent;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Control\Controller;

class MailingConstituentImportController extends Controller
{

    private static $allowed_actions = array(
        'ImportForm'
    );

    protected function init()
    {
        parent::init();
    }

    public function index(){
        $form = $this->ImportForm();

        return $this->customise(['Form' => $form])->renderWith('MailingConstituentImportController');
    }

    public function ImportForm()
    {
        $fields = FieldList::create();
        $fields->add( FileField::create('CsvFile', 'CSV to Import'));

        $actions = FieldList::create();
        $actions->add( FormAction::create('submit','Upload file' ));

        $form = Form::create($this, 'ImportForm', $fields, $actions);

        $form->setFormAction('submit');

        return $form;
    }

    public function submit($data, $form)
    {
       //Stub submit 
        $this->redirectBack();
    }

}

Any help would be greatly appreciated.

Fixed, will post answer to https://silverstrip.es soon!

It would be great to see your solution on this.

1 Like

I have the same problem. It would be nice to see your solution.

1 Like

Looks like I better get my writing hat on an get this uploaded here and on https://silverstrip.es too

1 Like

That would be massively appreciated :wink:

Just to clarify - the context I am looking for is s using GridFieldImportButton on a GridField within getCMSFields() on a DataObject.

@Ed_Gossage that’s what my solutin does - i’ll post later today

Would be nice if this solution was shared to help other developers like me. @mspacemedia

For anyone stumbling upon this and looking for a solution in the future: I’ve had the same problem and did make a post about the solution I came up with:

Enable CSV Import in Silverstripe GridFields | Syntro GmbH (It is built for One_Many, but the same can be done with Many_Many)

I hope this helps future lost souls :wink: