NEW Override automatic DataObject class

Specify which class to use when creating the DataObject that contains
the image.
This commit is contained in:
Thierry François 2016-01-18 16:49:00 +02:00
parent ef0f9fbcb8
commit af7524efd1
3 changed files with 50 additions and 39 deletions

View File

@ -7,10 +7,11 @@ Simplest usage, add the component to your `GridFieldConfig` as below. The compon
$config->addComponent(new GridFieldBulkUpload());
## Usage 2
You can specify which `Image` or `File` field to use.
You can specify which `Image` or `File` field to use and a specific `DataObject` class name to use.
$fileRelationName (string, optional): The name of the `Image` or `File` has_one field to use (If your relation is set has 'MyImage' => 'Image', the parameter should be 'MyImage')
$objectClassName (string, optional): The class name of the `DataObject` to create (Usefull if for example your `GridField` holds `DataObject`s of different classes, like when used with the `GridFieldAddNewMultiClass` component.)
$config->addComponent(new GridFieldBulkUpload($fileRelationName));
$config->addComponent(new GridFieldBulkUpload($fileRelationName, $objectClassName));
## Configuration
### Component configuration

View File

@ -7,14 +7,16 @@
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
{
/**
* component configuration.
* Component configuration.
*
* 'fileRelationName' => field name of the $has_one File/Image relation
* 'objectClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name
*
* @var array
*/
protected $config = array(
'fileRelationName' => null,
'objectClassName' => null
);
/**
@ -66,11 +68,15 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
*
* @param string $fileRelationName
*/
public function __construct($fileRelationName = null)
public function __construct($fileRelationName = null, $objectClassName = null)
{
if ($fileRelationName != null) {
$this->setConfig('fileRelationName', $fileRelationName);
}
if ($objectClassName != null) {
$this->setConfig('objectClassName', $objectClassName);
}
}
/* **********************************************************************

View File

@ -83,7 +83,11 @@ class GridFieldBulkUpload_Request extends RequestHandler
public function upload(SS_HTTPRequest $request)
{
//create record
$recordClass = $this->gridField->list->dataClass;
if ($this->component->getConfig('objectClassName')) {
$recordClass = $this->component->getConfig('objectClassName');
} else {
$recordClass = $this->gridField->list->dataClass;
}
$record = Object::create($recordClass);
$record->write();