Skip to content

Commit

Permalink
bureucracy: handle multi fields of type page with setting 'usetitles'
Browse files Browse the repository at this point in the history
Addresses one of the issues in #700
annda committed Jan 16, 2024
1 parent 6ad52e6 commit 30ad7b7
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion _test/BureaucracyTest.php
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
/**
* Tests for the integration with Bureaucracy plugin
*
* @group plugin_structb
* @group plugin_struct
* @group plugins
*
*/
7 changes: 4 additions & 3 deletions action/bureaucracy.php
Original file line number Diff line number Diff line change
@@ -115,9 +115,10 @@ public function handleLookupFields(Event $event, $param)
// lookups can reference pages or global data, so check both pid and rid
// make sure not to double decode pid!
$originalPid = $pid;
$pid = json_decode($pid, null, 512, JSON_THROW_ON_ERROR)[0] ?? $pid;
$rid = json_decode($originalPid, null, 512, JSON_THROW_ON_ERROR)[1] ?? null;
if (($pid && $pids[$i] === $pid) || ($rid && $rids[$i] === (string)$rid)) {
// do not throw JSON exception here, we supply alternative values if json_decode doesn't
$pid = json_decode($pid, null, 512)[0] ?? $pid;
$rid = json_decode($originalPid, null, 512)[1] ?? null;
if (($pid && $pids[$i] === $pid) || ($rid && $rids[$i] === $rid)) {
$field->opt['struct_pids'][] = $pid;
$new_value[] = $result[$i][0]->getDisplayValue();
}
9 changes: 7 additions & 2 deletions helper/field.php
Original file line number Diff line number Diff line change
@@ -158,14 +158,19 @@ public function replacementMultiValueCallback($matches)
*/
protected function createValue()
{
$preparedValue = $this->opt['value'] ?? '';
// input value or appropriately initialized empty value
$preparedValue = $this->opt['value'] ?? ($this->column->isMulti() ? [] : '');

// page fields might need to be JSON encoded depending on usetitles config
if (
$this->column->getType() instanceof Page
&& $this->column->getType()->getConfig()['usetitles']
) {
$preparedValue = json_encode([$preparedValue, null], JSON_THROW_ON_ERROR);
if ($this->column->isMulti()) {
$preparedValue = array_map(static fn($val) => json_encode([$val, null], JSON_THROW_ON_ERROR), $preparedValue);
} else {
$preparedValue = json_encode([$preparedValue, null], JSON_THROW_ON_ERROR);
}
}

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

0 comments on commit 30ad7b7

Please sign in to comment.