Skip to content

Commit 2b62a00

Browse files
authored
Merge pull request #701 from cosmocode/multi-usetitles
bureucracy: handle multi fields of type page with setting 'usetitles'
2 parents 6ad52e6 + 473a32c commit 2b62a00

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

_test/BureaucracyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Tests for the integration with Bureaucracy plugin
99
*
10-
* @group plugin_structb
10+
* @group plugin_struct
1111
* @group plugins
1212
*
1313
*/

action/bureaucracy.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ public function handleLookupFields(Event $event, $param)
115115
// lookups can reference pages or global data, so check both pid and rid
116116
// make sure not to double decode pid!
117117
$originalPid = $pid;
118-
$pid = json_decode($pid, null, 512, JSON_THROW_ON_ERROR)[0] ?? $pid;
119-
$rid = json_decode($originalPid, null, 512, JSON_THROW_ON_ERROR)[1] ?? null;
120-
if (($pid && $pids[$i] === $pid) || ($rid && $rids[$i] === (string)$rid)) {
118+
// do not throw JSON exception here, we supply alternative values if json_decode doesn't
119+
$pid = json_decode($pid, null, 512)[0] ?? $pid;
120+
$rid = json_decode($originalPid, null, 512)[1] ?? null;
121+
if (($pid && $pids[$i] === $pid) || ($rid && $rids[$i] === $rid)) {
121122
$field->opt['struct_pids'][] = $pid;
122123
$new_value[] = $result[$i][0]->getDisplayValue();
123124
}

helper/field.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,22 @@ public function replacementMultiValueCallback($matches)
158158
*/
159159
protected function createValue()
160160
{
161-
$preparedValue = $this->opt['value'] ?? '';
161+
// input value or appropriately initialized empty value
162+
$preparedValue = $this->opt['value'] ?? ($this->column->isMulti() ? [] : '');
162163

163164
// page fields might need to be JSON encoded depending on usetitles config
164165
if (
165166
$this->column->getType() instanceof Page
166167
&& $this->column->getType()->getConfig()['usetitles']
167168
) {
168-
$preparedValue = json_encode([$preparedValue, null], JSON_THROW_ON_ERROR);
169+
if ($this->column->isMulti()) {
170+
$preparedValue = array_map(
171+
static fn($val) => json_encode([$val, null], JSON_THROW_ON_ERROR),
172+
$preparedValue
173+
);
174+
} else {
175+
$preparedValue = json_encode([$preparedValue, null], JSON_THROW_ON_ERROR);
176+
}
169177
}
170178

171179
$value = new Value($this->column, $preparedValue);

0 commit comments

Comments
 (0)