Skip to content

Commit

Permalink
Merge pull request #294 from sminnee/allow-inline-dot-syntax
Browse files Browse the repository at this point in the history
FIX: Let GridFieldEditableColumns edit relations via dot syntax
  • Loading branch information
nyeholt authored Jun 17, 2020
2 parents 66d5e04 + 0b37e97 commit 2fc085b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/GridFieldAddNewInlineButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\ManyManyThroughList;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use Exception;
Expand Down Expand Up @@ -189,9 +190,15 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
foreach ($value[self::POST_KEY] as $fields) {
/** @var DataObject $item */
$item = $class::create();

// Add the item before the form is loaded so that the join-object is available
if ($list instanceof ManyManyThroughList) {
$list->add($item);
}

$extra = array();

$form = $editable->getForm($grid, $record);
$form = $editable->getForm($grid, $item);
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
$form->saveInto($item);

Expand All @@ -205,8 +212,12 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
}

$item->write();
$list->add($item, $extra);
$item->write(false, false, false, true);

// Add non-through lists after the write. many_many_extraFields are added there too
if (!($list instanceof ManyManyThroughList)) {
$list->add($item, $extra);
}
}
}
}
8 changes: 4 additions & 4 deletions src/GridFieldEditableColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public function getColumnContent($grid, $record, $col)
$field = clone $field;
} else {
$value = $grid->getDataFieldValue($record, $col);
$rel = (strpos($col, '.') === false); // field references a relation value
$field = ($rel) ? clone $fields->fieldByName($col) : new ReadonlyField($col);
$field = $fields->dataFieldByName($col);

if (!$field) {
throw new Exception("Could not find the field '$col'");
Expand Down Expand Up @@ -138,10 +137,11 @@ public function handleSave(GridField $grid, DataObjectInterface $record)

$extra = array();

$form = $this->getForm($grid, $record);
$form = $this->getForm($grid, $item);
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
$form->saveInto($item);


// Check if we are also sorting these records
if ($sortable) {
$sortField = $sortable->getSortField();
Expand All @@ -154,7 +154,7 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
}

$item->write();
$item->write(false, false, false, true);
$list->add($item, $extra);
}
}
Expand Down

0 comments on commit 2fc085b

Please sign in to comment.