Skip to content

Commit

Permalink
Improve buttons, add description, and add epic helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalker committed Aug 25, 2021
1 parent 43f4101 commit 380905f
Showing 1 changed file with 82 additions and 18 deletions.
100 changes: 82 additions & 18 deletions code/HasOneButtonField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,61 @@

class HasOneButtonField extends GridField
{
/**
* Helper function to add a HasOneButtonField to a tab, and have the added field be automatically added below
* the related DropdownField (if found).
* @param FieldList $fields
* @param string $tab
* @param string $name
* @param DataObject $parent
* @return void
*/
public static function addFieldToTab(FieldList $fields, string $tab, string $name, DataObject $parent): void
{
$fields->addFieldToTab($tab, self::create($name, $name, $parent));

$tabFieldList = $fields->fieldByName($tab)->Fields();
$tabFields = $tabFieldList->dataFields();
$hasOneButtonFields = [];
foreach ($tabFields as $fieldName => $field) {
if ($field->class === __CLASS__) {
$hasOneButtonFields[] = $fieldName;
}
}
$addedHasOneButtonFields = [];
$newFieldOrder = [];

/**
* @var string $fieldName
* @var FormField $field
*/
foreach ($tabFields as $fieldName => $field) {
if (in_array($fieldName, $addedHasOneButtonFields, true)) {
// Hide the border between the two related fields.
try {
$existingStyle = $field->getAttribute('style');
} catch (Throwable $e) {
$existingStyle = '';
}
$field->setAttribute('style', 'background: #f5f7f8; margin-top: -10px; ' . $existingStyle);
continue;
}
$matches = [];
if (preg_match("/^(.+?)ID$/", $fieldName, $matches)) {
$fieldNameLessId = $matches[1];
if (in_array($fieldNameLessId, $hasOneButtonFields, true)) {
$newFieldOrder[] = $fieldName;
$newFieldOrder[] = $fieldNameLessId;
$addedHasOneButtonFields[] = $fieldNameLessId;
continue;
}
}

$newFieldOrder[] = $fieldName;
}

$tabFieldList->changeFieldOrder($newFieldOrder);
}

protected $record;
protected $parent;
Expand All @@ -11,8 +66,8 @@ public function __construct($name, $title, $parent)
$this->record = $parent->{$name}();
$this->parent = $parent;
$config = GridFieldConfig::create()
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldHasOneEditButton());
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldHasOneEditButton());
$list = new HasOneButtonRelationList($this->record, $name, $parent);
parent::__construct($name, $title, $list, $config);
}
Expand All @@ -28,27 +83,36 @@ class GridFieldHasOneEditButton extends GridFieldAddNewButton implements GridFie

public function getHTMLFragments($gridField)
{
$record = $gridField->getRecord();
if (!$record->exists() || !$record->isInDB()) {
return parent::getHTMLFragments($gridField); //use parent add button
}
$singleton = singleton($gridField->getModelClass());
if (!$singleton->canCreate()) {
return array();
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.Edit', 'Edit {name}', array('name' => $objectName));
}
$data = new ArrayData(array(

$record = $gridField->getRecord();
$recordExists = $record->exists() && $record->isInDB();

$objectName = $singleton->i18n_singular_name();

$editButtonData = $recordExists ? new ArrayData([
'NewLink' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit'),
'ButtonName' => $this->buttonName,
));
'ButtonName' => 'Edit existing',
]) : null;

$newButtonData = new ArrayData([
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
'ButtonName' => $recordExists ? 'Unlink existing and add new' : 'Create and link new'
]);

$fragments = [
'before' => "Choose an existing <em>$objectName</em> from the above dropdown, or...<br />",
'after' => $newButtonData->renderWith('GridFieldAddNewbutton'),
];

if ($editButtonData) {
$fragments['before'] .= $editButtonData->renderWith('GridFieldAddNewbutton');
}

return array(
$this->targetFragment => $data->renderWith('GridFieldAddNewbutton')
);
return $fragments;
}
}

Expand Down

0 comments on commit 380905f

Please sign in to comment.