Skip to content

Commit

Permalink
#27 Customisable Question Identifiders
Browse files Browse the repository at this point in the history
  • Loading branch information
jakxnz committed Nov 12, 2020
1 parent f97b373 commit b16ef23
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/GridField/GridFieldConfig_CustomRelationEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SilverStripe\Versioned\Versioned;
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState;
use SilverStripe\Versioned\VersionedGridFieldStateExtension;
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;

/**
* A customised relation editor config for managing Pathfinder content
Expand All @@ -34,6 +35,7 @@ public function __construct($addButtonTitle = 'Add', $orderable = false)
$this->addComponents([
new GridFieldButtonRow('after'),
new GridFieldToolbarHeader(),
new GridFieldTitleHeader(),
(new \Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton('buttons-after-left'))
->setTitle($addButtonTitle),
new \Symbiote\GridFieldExtensions\GridFieldEditableColumns(),
Expand Down
9 changes: 6 additions & 3 deletions src/Model/Answer.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ public function getCMSFields()
if ($choicesField) {
$config = GridFieldConfig_CustomRelationEditor::create('Add Choice', true)
->setDisplayFields([
'ChoiceText' => function($record, $column, $grid) {
return TextField::create($column);
},
'ChoiceText' => [
'title' => 'Choice',
'callback' => function($record, $column, $grid) {
return TextField::create($column);
},
],
]);

$choicesField->setConfig($config);
Expand Down
16 changes: 14 additions & 2 deletions src/Model/Pathfinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,25 @@ public function getCMSFields()
if ($questionsField) {
$config = GridFieldConfig_CustomRelationEditor::create('Add Question', true)
->setDisplayFields([
'CMSID' => [
'field' => ReadonlyField::class,
'QID' => [
'title' => 'Question Identifider',
'callback' => function($record, $column, $grid) {
$placeholder = 'Leave blank to use default';

if ($record->ID) {
$placeholder = sprintf('%s (%s)', $record->ID, $placeholder);
}

return TextField::create($column)
->setAttribute('placeholder', $placeholder);
},
],
'QuestionText' => [
'title' => 'Question',
'field' => TextField::class,
],
'FlowTitle' => [
'title' => 'Flow',
'field' => ReadonlyField::class,
],
]);
Expand Down
32 changes: 27 additions & 5 deletions src/Model/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBString;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Versioned\Versioned;

/**
* A question used as a step in a Pathfinder
*
* @property string QID
* @property string QuestionText
* @property int Sort
* @method Pathfinder|null Pathfinder()
Expand All @@ -53,6 +56,7 @@ class Question extends DataObject
* @var array
*/
private static $db = [
'QID' => 'Varchar',
'QuestionText' => 'Text',
'Description' => 'HTMLText',
'Sort' => 'Int',
Expand Down Expand Up @@ -146,6 +150,14 @@ public function getCMSFields()
}
}

// Question identifider field
$qidField = $fields->dataFieldByName('QID');

$qidField
->setTitle('Question identifider')
->setAttribute('placeholder', $this->getDefaultQID())
->setDescription('Leave blank to use default');

// Quesiton text field
$fields->replaceField(
'QuestionText',
Expand Down Expand Up @@ -281,7 +293,7 @@ public function recursivePrecedentIDs($precedents = [])
*/
public function getFlowTitle()
{
return sprintf('Flow: %s', $this->Flow()->exists() ? $this->Flow()->Title : 'Default');
return $this->Flow()->exists() ? $this->Flow()->Title : 'Default';
}

/**
Expand All @@ -291,8 +303,8 @@ public function getCMSTitle()
{
$parts = [
$this->QuestionText,
' (Q#',
$this->ID
' (',
$this->QID ?: $this->getDefaultQID()
];

if ($this->Flow()) {
Expand All @@ -304,11 +316,21 @@ public function getCMSTitle()
return implode('', $parts);
}

/**
* Useful for identifying this question, (e.g {@see Pathfinder::getCMSFields()})
*
* @return string
*/
public function getCMSID()
{
return sprintf('Q: %s', $this->getDefaultQID());
}

/**
* @return string
*/
public function CMSID()
public function getDefaultQID()
{
return sprintf('Q#%s', $this->ID);
return $this->ID;
}
}

0 comments on commit b16ef23

Please sign in to comment.