diff --git a/module/VuFindApi/src/VuFindApi/Formatter/BaseFormatter.php b/module/VuFindApi/src/VuFindApi/Formatter/BaseFormatter.php index 96588b0bfa4f6ce0e51b61199e6ce91493c15f50..9c7d843d49e429737cdd1459b52f5d4787ec2982 100644 --- a/module/VuFindApi/src/VuFindApi/Formatter/BaseFormatter.php +++ b/module/VuFindApi/src/VuFindApi/Formatter/BaseFormatter.php @@ -56,7 +56,10 @@ class BaseFormatter $this->resetArrayIndices($value); } - if ((is_array($value) && empty($value)) + // We don't want to return empty values -- unless it's an empty array + // with a non-numeric key, since the key could be significant (e.g. in + // the case of an author name => roles array with no assigned roles). + if ((is_numeric($key) && is_array($value) && empty($value)) || (is_bool($value) && !$value) || $value === null || $value === '' ) { diff --git a/module/VuFindApi/tests/unit-tests/src/VuFindTest/Formatter/RecordFormatterTest.php b/module/VuFindApi/tests/unit-tests/src/VuFindTest/Formatter/RecordFormatterTest.php index 686cfb894d947102266bc96b5d32bc80eaf862fe..169642533af2417f06ced1a9f9211a27f72b0609 100644 --- a/module/VuFindApi/tests/unit-tests/src/VuFindTest/Formatter/RecordFormatterTest.php +++ b/module/VuFindApi/tests/unit-tests/src/VuFindTest/Formatter/RecordFormatterTest.php @@ -68,6 +68,7 @@ class RecordFormatterTest extends \VuFindTest\Unit\TestCase 'subjectsExtended' => [ 'vufind.method' => 'Formatter::getExtendedSubjectHeadings' ], + 'authors' => ['vufind.method' => 'getDeduplicatedAuthors'], ]; } @@ -121,6 +122,10 @@ class RecordFormatterTest extends \VuFindTest\Unit\TestCase 'spelling' => 's', 'Building' => ['foo', new TranslatableString('bar', 'xyzzy')], 'AllSubjectHeadings' => [['heading' => 'subject']], + 'DeduplicatedAuthors' => [ + 'primary' => ['Ms. A' => ['role' => ['Editor']]], + 'secondary' => ['Mr. B' => [], 'Mr. C' => []], + ], ] ); return $driver; @@ -158,6 +163,10 @@ class RecordFormatterTest extends \VuFindTest\Unit\TestCase 'buildings' => ['foo', ['value' => 'bar', 'translated' => 'xyzzy']], 'recordPage' => 'http://record', 'subjectsExtended' => [['heading' => 'subject']], + 'authors' => [ + 'primary' => ['Ms. A' => ['role' => ['Editor']]], + 'secondary' => ['Mr. B' => [], 'Mr. C' => []], + ], ], ]; $this->assertEquals($expected, $results); @@ -197,6 +206,7 @@ class RecordFormatterTest extends \VuFindTest\Unit\TestCase 'buildings' => [], 'recordPage' => [], 'subjectsExtended' => [], + 'authors' => [], ]; $this->assertEquals($expected, $results); }