FIX Make child responsible for its own nested gridfield

This commit is contained in:
Guy Sartorelli 2024-05-22 10:31:16 +12:00
parent 678ec6f7e1
commit 945ea7fc0b
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A

View File

@ -36,15 +36,6 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
public function ItemEditForm() public function ItemEditForm()
{ {
$config = new GridFieldConfig_RecordEditor();
/** @var GridFieldDetailForm */
$detailForm = $config->getComponentByType(GridFieldDetailForm::class);
$detailForm->setItemEditFormCallback(function (Form $form, $itemRequest) {
$breadcrumbs = $itemRequest->Breadcrumbs(false);
if ($breadcrumbs && $breadcrumbs->exists()) {
$form->Backlink = $breadcrumbs->first()->Link;
}
});
$relationName = $this->component->getRelationName(); $relationName = $this->component->getRelationName();
$list = $this->record->$relationName(); $list = $this->record->$relationName();
if ($relationName == 'Children' && $this->record->hasExtension(Hierarchy::class)) { if ($relationName == 'Children' && $this->record->hasExtension(Hierarchy::class)) {
@ -55,10 +46,14 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
->forForeignID($this->record->ID); ->forForeignID($this->record->ID);
} }
$relationClass = $list->dataClass(); $relationClass = $list->dataClass();
$singleton = singleton($relationClass);
if ($this->record->hasMethod('getNestedConfig')) { $config = null;
$config = $this->record->getNestedConfig(); if ($singleton->hasMethod('getNestedConfig')) {
} else { $config = $singleton->getNestedConfig(get_class($this->record), $relationName);
}
if (!$config) {
$config = new GridFieldConfig_RecordEditor();
$canEdit = $this->record->canEdit(); $canEdit = $this->record->canEdit();
if (!$canEdit) { if (!$canEdit) {
$config->removeComponentsByType(GridFieldAddNewButton::class); $config->removeComponentsByType(GridFieldAddNewButton::class);
@ -97,6 +92,14 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
} }
} }
} }
/** @var GridFieldDetailForm */
$detailForm = $config->getComponentByType(GridFieldDetailForm::class);
$detailForm->setItemEditFormCallback(function (Form $form, $itemRequest) {
$breadcrumbs = $itemRequest->Breadcrumbs(false);
if ($breadcrumbs && $breadcrumbs->exists()) {
$form->Backlink = $breadcrumbs->first()->Link;
}
});
$this->record->invokeWithExtensions('updateNestedConfig', $config); $this->record->invokeWithExtensions('updateNestedConfig', $config);