Compare commits

...

2 Commits

Author SHA1 Message Date
Niklas Forsdahl cfcf8d2e8e Refactored nested GridField move to parent functionality. 2024-04-23 17:03:00 +03:00
Niklas Forsdahl 32d980e13c Fixed moving nested gridfield items to other gridfields 2024-04-23 16:35:10 +03:00
2 changed files with 52 additions and 43 deletions

View File

@ -1,4 +1,7 @@
(function($) { (function($) {
let preventReorderUpdate = false;
let updateTimeouts = [];
$.entwine("ss", function($) { $.entwine("ss", function($) {
/** /**
* GridFieldAddExistingSearchButton * GridFieldAddExistingSearchButton
@ -612,8 +615,6 @@
$('.ss-gridfield-orderable.has-nested > .grid-field__table > tbody, .ss-gridfield-orderable.nested > .grid-field__table > tbody').entwine({ $('.ss-gridfield-orderable.has-nested > .grid-field__table > tbody, .ss-gridfield-orderable.nested > .grid-field__table > tbody').entwine({
onadd: function() { onadd: function() {
this._super(); this._super();
let preventReorderUpdate = false;
let updateTimeouts = [];
let gridField = this.getGridField(); let gridField = this.getGridField();
if (gridField.data("url-movetoparent")) { if (gridField.data("url-movetoparent")) {
let parentID = 0; let parentID = 0;

View File

@ -266,53 +266,61 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** @var DataList */ /** @var DataList */
$list = $gridField->getList(); $list = $gridField->getList();
$id = isset($move['id']) ? (int) $move['id'] : null; $id = isset($move['id']) ? (int) $move['id'] : null;
if (!$id) {
throw new HTTPResponse_Exception('Missing ID', 400);
}
$to = isset($move['parent']) ? (int)$move['parent'] : null; $to = isset($move['parent']) ? (int)$move['parent'] : null;
$parent = null; // should be possible either on parent or child grid field, or nested grid field from parent
if ($id) { $parent = $to ? $list->byID($to) : null;
// should be possible either on parent or child grid field, or nested grid field from parent if (!$parent
$parent = $to ? $list->byID($to) : null; && $to
if (!$parent && $gridField->getForm()->getController() instanceof GridFieldNestedFormItemRequest
&& $to && $gridField->getForm()->getController()->getRecord()->ID == $to
&& $gridField->getForm()->getController() instanceof GridFieldNestedFormItemRequest ) {
&& $gridField->getForm()->getController()->getRecord()->ID == $to $parent = $gridField->getForm()->getController()->getRecord();
) { }
$parent = $gridField->getForm()->getController()->getRecord(); $child = $list->byID($id);
// we need either a parent or a child, or a move to top level at this stage
if (!($parent || $child || $to === 0)) {
throw new HTTPResponse_Exception('Invalid request', 400);
}
// parent or child might be from another grid field, so we need to search via DataList in some cases
if (!$parent && $to) {
$parent = DataList::create($gridField->getModelClass())->byID($to);
}
if (!$child) {
$child = DataList::create($gridField->getModelClass())->byID($id);
}
if ($child) {
if (!$child->canEdit()) {
throw new HTTPResponse_Exception('Not allowed', 403);
} }
$child = $list->byID($id); if ($child->hasExtension(Hierarchy::class)) {
if ($parent || $child || $to === 0) { $child->ParentID = $parent ? $parent->ID : 0;
if (!$parent && $to) { }
$parent = DataList::create($gridField->getModelClass())->byID($to); // validate that the record is still valid
$validationResult = $child->validate();
if ($validationResult->isValid()) {
if ($child->hasExtension(Versioned::class)) {
$child->writeToStage(Versioned::DRAFT);
} else {
$child->write();
} }
if (!$child) {
$child = DataList::create($gridField->getModelClass())->byID($id);
}
if ($child) {
if ($child->hasExtension(Hierarchy::class)) {
$child->ParentID = $parent ? $parent->ID : 0;
}
$validationResult = $child->validate();
if ($validationResult->isValid()) {
if ($child->hasExtension(Versioned::class)) {
$child->writeToStage(Versioned::DRAFT);
} else {
$child->write();
}
/** @var GridFieldOrderableRows */ // reorder items at the same time, if applicable
$orderableRows = $gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class); /** @var GridFieldOrderableRows */
if ($orderableRows) { $orderableRows = $gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class);
$orderableRows->setImmediateUpdate(true); if ($orderableRows) {
try { $orderableRows->setImmediateUpdate(true);
$orderableRows->handleReorder($gridField, $request); try {
} catch (Exception $e) { $orderableRows->handleReorder($gridField, $request);
} } catch (Exception $e) {
}
} else {
$messages = $validationResult->getMessages();
$message = array_pop($messages);
throw new HTTPResponse_Exception($message['message'], 400);
} }
} }
} else {
$messages = $validationResult->getMessages();
$message = array_pop($messages);
throw new HTTPResponse_Exception($message['message'], 400);
} }
} }
return $gridField->FieldHolder(); return $gridField->FieldHolder();