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,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();