Skip to content

Commit

Permalink
Fix bug with unlink action not re-rendering field
Browse files Browse the repository at this point in the history
  • Loading branch information
hchokshi committed Jul 30, 2018
1 parent c0dca04 commit a962987
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
25 changes: 15 additions & 10 deletions src/GridFieldHasOneEditButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,29 @@ public function getHTMLFragments($gridField)
if (!$record->exists() || !$record->isInDB()) {
return parent::getHTMLFragments($gridField); //use parent add button
}

$singleton = singleton($gridField->getModelClass());
if (!$singleton->canCreate()) {
return array();
}
if (!$singleton->canCreate()) return [];

if (!$this->buttonName) {
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
$objectName = $singleton->i18n_singular_name();
$this->buttonName = _t(GridField::class . '.Edit', 'Edit {name}', array('name' => $objectName));
$buttonName = $gridField->getRecord()->exists()
? _t(GridField::class . '.Edit', 'Edit {name}', ['name' => $objectName])
: _t(GridField::class . '.Add', 'Add {name}', ['name' => $objectName]);

$this->setButtonName($buttonName);
}

$data = ArrayData::create(
array(
'NewLink' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit'),
[
'NewLink' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit'),
'ButtonName' => $this->buttonName,
)
]
);

return array(
$this->targetFragment => $data->renderWith(SSViewer::get_templates_by_class(static::class))
);
return [
$this->targetFragment => $data->renderWith(SSViewer::get_templates_by_class(static::class)),
];
}
}
1 change: 1 addition & 0 deletions src/GridFieldHasOneUnlinkButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
);
}

$gridField->setRecord(null);
$gridField->getList()->remove($item);

Controller::curr()->getResponse()->setStatusCode(
Expand Down
18 changes: 8 additions & 10 deletions src/HasOneButtonField.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ public function __construct(DataObject $parent, $relationName, $fieldName = null
$list = new HasOneButtonRelationList($this->record, $relationName, $parent);

parent::__construct($fieldName ?: $relationName, $title, $list, $config);

if ($title !== null) {
$buttonName = $this->getRecord()->exists()
? _t(GridField::class . '.Edit', 'Edit {name}', ['name' => $title])
: _t(GridField::class . '.Add', 'Add {name}', ['name' => $title]);

/** @var \SilverShop\HasOneField\GridFieldHasOneEditButton $editButton */
$editButton = $this->getConfig()->getComponentByType(GridFieldHasOneEditButton::class);
$editButton->setButtonName($buttonName);
}
}

/**
Expand All @@ -54,4 +44,12 @@ public function getRecord()
{
return $this->record;
}

/**
* @param DataObject|null $record
*/
public function setRecord($record): void
{
$this->record = $record ?: singleton(get_class($this->record));
}
}

0 comments on commit a962987

Please sign in to comment.