mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 09:05:57 +00:00
NEW Override automatic DataObject class
Specify which class to use when creating the DataObject that contains the image.
This commit is contained in:
parent
ef0f9fbcb8
commit
78967304a2
@ -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')
|
||||
$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
|
||||
### Component configuration
|
||||
|
@ -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
|
||||
* 'recordClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = array(
|
||||
'fileRelationName' => null,
|
||||
'recordClassName' => 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, $recordClassName = null)
|
||||
{
|
||||
if ($fileRelationName != null) {
|
||||
$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
|
||||
* i.e. 'MyImage' => 'Image' will return 'MyImage'.
|
||||
@ -215,7 +232,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
|
||||
*/
|
||||
public function getDefaultFileRelationName($gridField)
|
||||
{
|
||||
$recordClass = $gridField->list->dataClass;
|
||||
$recordClass = $this->getRecordClassName($gridField);
|
||||
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
||||
|
||||
$imageField = null;
|
||||
@ -251,7 +268,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
|
||||
*/
|
||||
public function getFileRelationClassName($gridField)
|
||||
{
|
||||
$recordClass = $gridField->list->dataClass;
|
||||
$recordClass = $this->getRecordClassName($gridField);
|
||||
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
||||
|
||||
$fieldName = $this->getFileRelationName($gridField);
|
||||
|
@ -83,7 +83,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
|
||||
public function upload(SS_HTTPRequest $request)
|
||||
{
|
||||
//create record
|
||||
$recordClass = $this->gridField->list->dataClass;
|
||||
$recordClass = $this->component->getRecordClassName($this->gridField);
|
||||
$record = Object::create($recordClass);
|
||||
$record->write();
|
||||
|
||||
@ -226,7 +226,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
|
||||
$attachResponses = json_decode($attachResponses->getBody(), true);
|
||||
|
||||
$fileRelationName = $uploadField->getName();
|
||||
$recordClass = $this->gridField->list->dataClass;
|
||||
$recordClass = $this->component->getRecordClassName($this->gridField);
|
||||
$return = array();
|
||||
|
||||
foreach ($attachResponses as $attachResponse) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user