Skip to content

Commit d1482d4

Browse files
committed
Handle 'usetitles' config of page fields in bureaucracy forms
If 'usetitles' is true, the struct field expects a JSON encoded value with both id and title, otherwise an exception is thrown. This is a followup to #128 and it fixes #553
1 parent d4a204b commit d1482d4

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

helper/field.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use dokuwiki\plugin\struct\meta\Value;
77
use dokuwiki\plugin\struct\meta\ValueValidator;
88
use dokuwiki\plugin\struct\types\Lookup;
9+
use dokuwiki\plugin\struct\types\Page;
910

1011
/**
1112
* Allows adding a single struct field as a bureaucracy field
@@ -96,14 +97,39 @@ public function renderfield($params, Doku_Form $form, $formid)
9697
}
9798

9899
// output the field
99-
$value = new Value($this->column, $this->opt['value']);
100-
if ($this->column->getType() instanceof Lookup) {
101-
$value->setValue($this->opt['value'], true);
102-
}
100+
$value = $this->createValue();
103101
$field = $this->makeField($value, $params['name']);
104102
$form->addElement($field);
105103
}
106104

105+
/**
106+
* Returns a Value object for the current column.
107+
* Special handling for Page and Lookup literal form values.
108+
*
109+
* @return Value
110+
*/
111+
protected function createValue()
112+
{
113+
$preparedValue = $this->opt['value'];
114+
115+
// page fields might need to be JSON encoded depending on usetitles config
116+
if (
117+
$this->column->getType() instanceof Page
118+
&& $this->column->getType()->getConfig()['usetitles']
119+
) {
120+
$preparedValue = json_encode([$this->opt['value'], null]);
121+
}
122+
123+
$value = new Value($this->column, $preparedValue);
124+
125+
// no way to pass $israw parameter to constructor, so re-set the Lookup value
126+
if ($this->column->getType() instanceof Lookup) {
127+
$value->setValue($preparedValue, true);
128+
}
129+
130+
return $value;
131+
}
132+
107133
/**
108134
* Create the input field
109135
*

0 commit comments

Comments
 (0)