Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Make child responsible for its own nested gridfield #393

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/GridFieldNestedFormItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,9 @@ public function Link($action = null)
{
return Controller::join_links($this->component->Link($this->record->ID), $action);
}

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();
$list = $this->record->$relationName();
if ($relationName == 'Children' && $this->record->hasExtension(Hierarchy::class)) {
Expand All @@ -55,10 +46,14 @@ public function ItemEditForm()
->forForeignID($this->record->ID);
}
$relationClass = $list->dataClass();
$singleton = singleton($relationClass);

if ($this->record->hasMethod('getNestedConfig')) {
$config = $this->record->getNestedConfig();
} else {
$config = null;
if ($singleton->hasMethod('getNestedConfig')) {
$config = $singleton->getNestedConfig(get_class($this->record), $relationName);
}
if (!$config) {
$config = new GridFieldConfig_RecordEditor();
$canEdit = $this->record->canEdit();
if (!$canEdit) {
$config->removeComponentsByType(GridFieldAddNewButton::class);
Expand Down Expand Up @@ -97,11 +92,19 @@ public function ItemEditForm()
}
}
}
/** @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);

$title = _t(get_class($this->record).'.'.strtoupper($relationName), ' ');

$fields = new FieldList(
$gridField = new GridField(
sprintf(
Expand Down Expand Up @@ -133,13 +136,13 @@ public function ItemEditForm()
$this->record->extend('updateNestedForm', $form);
return $form;
}

public function Breadcrumbs($unlinked = false)
{
if (!$this->popupController->hasMethod('Breadcrumbs')) {
return null;
}

/** @var ArrayList $items */
$items = $this->popupController->Breadcrumbs($unlinked);

Expand Down
Loading