Skip to content

Commit

Permalink
Only load linkfield when object exists and move CMSFields to beforeUp…
Browse files Browse the repository at this point in the history
…date hook
  • Loading branch information
mlewis-everley committed Jan 16, 2020
1 parent 0b8452f commit 4d76733
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ This file logs major changes for the SilverStripe System Mesages module.

## 1.0.1

* Minor updates to readme and travis config
* Minor updates to readme and travis config

## 2.0.0

* Upgrade to Silverstripe 4
* Add ability to add a delay (in seconds) to the modal
38 changes: 28 additions & 10 deletions src/model/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ class SystemMessage extends DataObject
);

private static $has_one = array(
'Link' => Link::class
'Link' => Link::class
);

private static $belongs_many_many = array(
"ClosedBy" => Member::class
);

private static $field_labels = array(
'Link' => 'Link to page or file'
);

private static $summary_fields = array(
"Content.Summary" => "Content",
"StartDate" => "Starts",
Expand Down Expand Up @@ -150,15 +154,29 @@ public function UseBootstrap()

public function getCMSFields()
{
$fields = parent::getCMSFields();

$fields->removeByName('LinkID');

$fields->addFieldToTab(
'Root.Main',
LinkField::create('Link', 'Link to page or file', $this)
$self = $this;

$this->beforeUpdateCMSFields(
function ($fields) use ($self) {
$fields->removeByName('LinkID');

// Add description to delay field
$delay_field = $fields->dataFieldByName('Delay');
if (!empty($delay_field)) {
$delay_field->setDescription(
_t(__CLASS__ . ".DelayDescription", 'Delay in seconds to open this message')
);
}

if ($self->exists()) {
$fields->addFieldToTab(
'Root.Main',
LinkField::create('Link', $self->fieldLabel('Link'), $this)
);
}
}
);

return $fields;
return parent::getCMSFields();
}
}
}

0 comments on commit 4d76733

Please sign in to comment.