Skip to content

Commit

Permalink
[4.x] Filter away bad bard nodes during preprocessing (statamic#9608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvester Damgaard authored Feb 28, 2024
1 parent 901fa9d commit 341c86f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ protected function processRow($row, $index)

public function preProcess($value)
{
// Filter out broken nodes
if (is_array($value)) {
$value = collect($value)->filter(function ($node) {
return array_key_exists('type', $node);
})->values()->all();
}

if (empty($value) || $value === '[]') {
return '[]';
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Fieldtypes/BardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,20 @@ public function augment($value)
$this->assertEquals('test.-1.words', $value['defaults']['one']['words']);
}

/** @test */
public function it_filters_away_bad_nodes()
{
$data = [
[],
['type' => 'text', 'text' => 'This is inline text.'],
['text' => 'I have no type'],
];

$expected = '[{"type":"paragraph","content":[{"type":"text","text":"This is inline text."}]}]';

$this->assertEquals($expected, $this->bard(['input_mode' => 'block', 'sets' => null])->preProcess($data));
}

private function bard($config = [])
{
return (new Bard)->setField(new Field('test', array_merge(['type' => 'bard', 'sets' => ['one' => []]], $config)));
Expand Down

0 comments on commit 341c86f

Please sign in to comment.