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 78967304a2
3 changed files with 60 additions and 42 deletions

View File

@ -7,10 +7,11 @@ Simplest usage, add the component to your `GridFieldConfig` as below. The compon
$config->addComponent(new GridFieldBulkUpload()); $config->addComponent(new GridFieldBulkUpload());
## Usage 2 ## 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') $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')
$recordClassName (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, $recordClassName));
## Configuration ## Configuration
### Component configuration ### Component configuration

View File

@ -7,14 +7,16 @@
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
{ {
/** /**
* component configuration. * Component configuration.
* *
* 'fileRelationName' => field name of the $has_one File/Image relation * 'fileRelationName' => field name of the $has_one File/Image relation
* 'recordClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name
* *
* @var array * @var array
*/ */
protected $config = array( protected $config = array(
'fileRelationName' => null, 'fileRelationName' => null,
'recordClassName' => null
); );
/** /**
@ -66,11 +68,15 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* *
* @param string $fileRelationName * @param string $fileRelationName
*/ */
public function __construct($fileRelationName = null) public function __construct($fileRelationName = null, $recordClassName = null)
{ {
if ($fileRelationName != null) { if ($fileRelationName != null) {
$this->setConfig('fileRelationName', $fileRelationName); $this->setConfig('fileRelationName', $fileRelationName);
} }
if ($recordClassName != null) {
$this->setConfig('recordClassName', $recordClassName);
}
} }
/* ********************************************************************** /* **********************************************************************
@ -207,6 +213,17 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
} }
} }
/**
* Returns the class name of container `DataObject` record.
* Either as set in the component config or fron the `Gridfield` `dataClass.
*
* @return string
*/
public function getRecordClassName($gridField)
{
return $this->getConfig('recordClassName') ? $this->getConfig('recordClassName') : $gridField->list->dataClass;
}
/** /**
* Get the first has_one Image/File relation from the GridField managed DataObject * Get the first has_one Image/File relation from the GridField managed DataObject
* i.e. 'MyImage' => 'Image' will return 'MyImage'. * i.e. 'MyImage' => 'Image' will return 'MyImage'.
@ -215,7 +232,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
*/ */
public function getDefaultFileRelationName($gridField) public function getDefaultFileRelationName($gridField)
{ {
$recordClass = $gridField->list->dataClass; $recordClass = $this->getRecordClassName($gridField);
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED); $hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$imageField = null; $imageField = null;
@ -251,7 +268,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
*/ */
public function getFileRelationClassName($gridField) public function getFileRelationClassName($gridField)
{ {
$recordClass = $gridField->list->dataClass; $recordClass = $this->getRecordClassName($gridField);
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED); $hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$fieldName = $this->getFileRelationName($gridField); $fieldName = $this->getFileRelationName($gridField);

View File

@ -83,7 +83,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
public function upload(SS_HTTPRequest $request) public function upload(SS_HTTPRequest $request)
{ {
//create record //create record
$recordClass = $this->gridField->list->dataClass; $recordClass = $this->component->getRecordClassName($this->gridField);
$record = Object::create($recordClass); $record = Object::create($recordClass);
$record->write(); $record->write();
@ -226,7 +226,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
$attachResponses = json_decode($attachResponses->getBody(), true); $attachResponses = json_decode($attachResponses->getBody(), true);
$fileRelationName = $uploadField->getName(); $fileRelationName = $uploadField->getName();
$recordClass = $this->gridField->list->dataClass; $recordClass = $this->component->getRecordClassName($this->gridField);
$return = array(); $return = array();
foreach ($attachResponses as $attachResponse) { foreach ($attachResponses as $attachResponse) {