Skip to content

Commit

Permalink
FIX: Don't attempt to render the FirstStep field for a decision tree …
Browse files Browse the repository at this point in the history
…if it is not saved in the database yet

This fixes an issue where decision trees are created outside of an ElementalArea context
  • Loading branch information
scott1702 committed Sep 12, 2022
1 parent d4fbdcc commit 8135f7b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Model/ElementDecisionTree.php
Expand Up @@ -7,6 +7,7 @@
use DNADesign\SilverStripeElementalDecisionTree\Forms\DecisionTreeStepPreview;
use SilverStripe\Control\Controller;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Forms\LiteralField;

class ElementDecisionTree extends BaseElement
{
Expand Down Expand Up @@ -38,18 +39,27 @@ public function getType()
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('FirstStepID');

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

$fields->removeByName('FirstStepID');
$stepSelector = HasOneSelectOrCreateField::create(
$this, 'FirstStep', 'First Step', DecisionTreeStep::get_initial_steps()->map(), $this->FirstStep(), $this
);
if ($this->IsInDB()) {
$stepSelector = HasOneSelectOrCreateField::create(
$this, 'FirstStep', 'First Step', DecisionTreeStep::get_initial_steps()->map(), $this->FirstStep(), $this
);

$fields->addFieldToTab('Root.Main', $stepSelector);

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

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

return $fields;
}
Expand Down

0 comments on commit 8135f7b

Please sign in to comment.