Changed naming schema for nested GridFields, to not include [ or ]

characters. This makes them work also with the search component.
This commit is contained in:
Niklas Forsdahl 2024-04-18 14:39:25 +03:00
parent c415d43731
commit f8c777dcc5
3 changed files with 19 additions and 12 deletions

View File

@ -612,6 +612,8 @@
$('.ss-gridfield-orderable.has-nested > .grid-field__table > tbody, .ss-gridfield-orderable.nested > .grid-field__table > tbody').entwine({
onadd: function() {
this._super();
let preventReorderUpdate = false;
let updateTimeouts = [];
let gridField = this.getGridField();
if (gridField.data("url-movetoparent")) {
let parentID = 0;
@ -638,7 +640,7 @@
window.clearTimeout(timeout);
}
let childID = ui.item.attr('data-id');
let parentIntoChild = $(e.target).closest('.grid-field[data-name*="[GridFieldNestedForm]['+childID+']"]').length;
let parentIntoChild = $(e.target).closest('.grid-field[data-name*="-GridFieldNestedForm-'+childID+'"]').length;
if (parentIntoChild) {
// parent dragged into child, cancel sorting
ui.sender.sortable("cancel");

View File

@ -359,11 +359,8 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
public function handleSave(GridField $gridField, DataObjectInterface $record)
{
$postKey = self::POST_KEY;
$value = $gridField->Value();
if (!isset($value[self::POST_KEY]) || !is_array($value[self::POST_KEY])) {
return;
}
if (isset($value['GridState']) && $value['GridState']) {
// set grid state from value, to store open/closed toggle state for nested forms
$gridField->getState(false)->setValue($value['GridState']);
@ -373,12 +370,16 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) {
$gridField->getState(false)->setValue($gridStateStr);
}
foreach ($value[self::POST_KEY] as $recordID => $nestedData) {
$record = $gridField->getList()->byID($recordID);
if ($record) {
$nestedGridField = $this->handleNestedItem($gridField, null, $record);
$nestedGridField->setValue($nestedData);
$nestedGridField->saveInto($record);
foreach ($request->postVars() as $key => $val) {
if (preg_match("/{$gridField->getName()}-{$postKey}-(\d+)/", $key, $matches)) {
$recordID = $matches[1];
$nestedData = $val;
$record = $gridField->getList()->byID($recordID);
if ($record) {
$nestedGridField = $this->handleNestedItem($gridField, null, $record);
$nestedGridField->setValue($nestedData);
$nestedGridField->saveInto($record);
}
}
}
}

View File

@ -65,6 +65,10 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
if ($relationClass == get_class($this->record)) {
$config->removeComponentsByType(GridFieldSortableHeader::class);
$config->removeComponentsByType(GridFieldFilterHeader::class);
if ($this->gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class)) {
$config->addComponent(new GridFieldOrderableRows());
}
}
if ($this->record->hasExtension(Hierarchy::class)) {
@ -96,7 +100,7 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
$fields = new FieldList(
$gridField = new GridField(
sprintf(
'%s[%s][%s]',
'%s-%s-%s',
$this->component->getGridField()->getName(),
GridFieldNestedForm::POST_KEY,
$this->record->ID