Refactored nested GridField move to parent functionality.

This commit is contained in:
Niklas Forsdahl 2024-04-23 17:03:00 +03:00
parent 32d980e13c
commit cfcf8d2e8e
1 changed files with 49 additions and 41 deletions

View File

@ -266,9 +266,10 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** @var DataList */
$list = $gridField->getList();
$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;
$parent = null;
if ($id) {
// should be possible either on parent or child grid field, or nested grid field from parent
$parent = $to ? $list->byID($to) : null;
if (!$parent
@ -279,7 +280,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$parent = $gridField->getForm()->getController()->getRecord();
}
$child = $list->byID($id);
if ($parent || $child || $to === 0) {
// 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);
}
@ -287,9 +292,13 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$child = DataList::create($gridField->getModelClass())->byID($id);
}
if ($child) {
if (!$child->canEdit()) {
throw new HTTPResponse_Exception('Not allowed', 403);
}
if ($child->hasExtension(Hierarchy::class)) {
$child->ParentID = $parent ? $parent->ID : 0;
}
// validate that the record is still valid
$validationResult = $child->validate();
if ($validationResult->isValid()) {
if ($child->hasExtension(Versioned::class)) {
@ -298,6 +307,7 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$child->write();
}
// reorder items at the same time, if applicable
/** @var GridFieldOrderableRows */
$orderableRows = $gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class);
if ($orderableRows) {
@ -313,8 +323,6 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
throw new HTTPResponse_Exception($message['message'], 400);
}
}
}
}
return $gridField->FieldHolder();
}