Skip to content

Commit 8135f7b

Browse files
committed
FIX: Don't attempt to render the FirstStep field for a decision tree if it is not saved in the database yet
This fixes an issue where decision trees are created outside of an ElementalArea context
1 parent d4fbdcc commit 8135f7b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/Model/ElementDecisionTree.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DNADesign\SilverStripeElementalDecisionTree\Forms\DecisionTreeStepPreview;
88
use SilverStripe\Control\Controller;
99
use SilverStripe\CMS\Controllers\CMSPageEditController;
10+
use SilverStripe\Forms\LiteralField;
1011

1112
class ElementDecisionTree extends BaseElement
1213
{
@@ -38,18 +39,27 @@ public function getType()
3839
public function getCMSFields()
3940
{
4041
$fields = parent::getCMSFields();
42+
$fields->removeByName('FirstStepID');
4143

4244
$introduction = $fields->dataFieldByName('Introduction');
4345
$introduction->setRows(4);
4446

45-
$fields->removeByName('FirstStepID');
46-
$stepSelector = HasOneSelectOrCreateField::create(
47-
$this, 'FirstStep', 'First Step', DecisionTreeStep::get_initial_steps()->map(), $this->FirstStep(), $this
48-
);
47+
if ($this->IsInDB()) {
48+
$stepSelector = HasOneSelectOrCreateField::create(
49+
$this, 'FirstStep', 'First Step', DecisionTreeStep::get_initial_steps()->map(), $this->FirstStep(), $this
50+
);
51+
52+
$fields->addFieldToTab('Root.Main', $stepSelector);
4953

50-
$fields->addFieldToTab('Root.Main', $stepSelector);
54+
$fields->addFieldToTab('Root.Tree', DecisionTreeStepPreview::create('Tree', $this->FirstStep()));
55+
} else {
56+
$info = LiteralField::create('info', sprintf(
57+
'<p class="message info notice">%s</p>',
58+
'Save this decision tree in order to add the first step.'
59+
));
5160

52-
$fields->addFieldToTab('Root.Tree', DecisionTreeStepPreview::create('Tree', $this->FirstStep()));
61+
$fields->addFieldToTab('Root.Main', $info);
62+
}
5363

5464
return $fields;
5565
}

0 commit comments

Comments
 (0)