From 2fe30aec0070d08e5f2d137e470f68937cb801ec Mon Sep 17 00:00:00 2001 From: Ere Maijala <ere.maijala@helsinki.fi> Date: Thu, 20 Apr 2017 19:53:46 +0300 Subject: [PATCH] Make RecordDataFormatter->getData() return arrays instead of HTML (#959) - This improves extensibility by allowing the insertion of more values for custom rendering, instead of a flat HTML string. --- .../View/Helper/Root/RecordDataFormatter.php | 8 ++++++-- .../View/Helper/Root/RecordDataFormatterTest.php | 12 +++++++++--- .../RecordDriver/SolrDefault/collection-info.phtml | 4 ++-- .../SolrDefault/collection-record.phtml | 4 ++-- .../templates/RecordDriver/SolrDefault/core.phtml | 4 ++-- .../RecordDriver/SolrDefault/data-authors.phtml | 14 ++++++++------ .../templates/RecordTab/description.phtml | 4 ++-- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php index b186f4f2eb4..cf5e262c106 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php @@ -73,7 +73,7 @@ class RecordDataFormatter extends AbstractHelper * @param RecordDriver $driver Record driver object. * @param array $spec Formatting specification * - * @return Record + * @return array */ public function getData(RecordDriver $driver, array $spec) { @@ -105,7 +105,11 @@ class RecordDataFormatter extends AbstractHelper ) { $field = call_user_func($current['labelFunction'], $data); } - $result[$field] = $text; + $context = isset($current['context']) ? $current['context'] : []; + $result[$field] = [ + 'value' => $text, + 'context' => $context + ]; } } } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php index fe2e7491562..4f5ee484276 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php @@ -157,7 +157,9 @@ class RecordDataFormatterTest extends \VuFindTest\Unit\ViewHelperTestCase { $formatter = $this->getFormatter(); $spec = $formatter->getDefaults('core'); - $spec['Building'] = ['dataMethod' => 'getBuilding', 'pos' => 0]; + $spec['Building'] = [ + 'dataMethod' => 'getBuilding', 'pos' => 0, 'context' => ['foo' => 1] + ]; $expected = [ 'Building' => '0', @@ -182,11 +184,15 @@ class RecordDataFormatterTest extends \VuFindTest\Unit\ViewHelperTestCase // Check for expected text (with markup stripped) foreach ($expected as $key => $value) { $this->assertEquals( - $value, trim(preg_replace('/\s+/', ' ', strip_tags($results[$key]))) + $value, + trim(preg_replace('/\s+/', ' ', strip_tags($results[$key]['value']))) ); } // Check for exact markup in representative example: - $this->assertEquals('Italian<br />Latin', $results['Language']); + $this->assertEquals('Italian<br />Latin', $results['Language']['value']); + + // Check for context in Building: + $this->assertEquals(['foo' => 1], $results['Building']['context']); } } diff --git a/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-info.phtml b/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-info.phtml index 670ca115ead..dc91ed04a70 100644 --- a/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-info.phtml +++ b/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-info.phtml @@ -47,8 +47,8 @@ ?> <? if (!empty($fields)): ?> <table id="collectionInfo" class="table table-striped" summary="<?=$this->transEsc('Bibliographic Details')?>"> - <? foreach ($fields as $key => $value): ?> - <tr><th><?=$this->transEsc($key)?>:</th><td><?=$value?></td></tr> + <? foreach ($fields as $key => $current): ?> + <tr><th><?=$this->transEsc($key)?>:</th><td><?=$current['value']?></td></tr> <? endforeach; ?> </table> <? endif; ?> diff --git a/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-record.phtml b/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-record.phtml index 93d236d6438..64ce05a7b5d 100644 --- a/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-record.phtml +++ b/themes/bootstrap3/templates/RecordDriver/SolrDefault/collection-record.phtml @@ -7,8 +7,8 @@ ?> <? if (!empty($fields)): ?> <table class="table table-striped" summary="<?=$this->transEsc('Bibliographic Details')?>"> - <? foreach ($fields as $key => $value): ?> - <tr><th><?=$this->transEsc($key)?>:</th><td><?=$value?></td></tr> + <? foreach ($fields as $key => $current): ?> + <tr><th><?=$this->transEsc($key)?>:</th><td><?=$current['value']?></td></tr> <? endforeach; ?> </table> <? endif; ?> diff --git a/themes/bootstrap3/templates/RecordDriver/SolrDefault/core.phtml b/themes/bootstrap3/templates/RecordDriver/SolrDefault/core.phtml index 4504c410944..464e5a00b12 100644 --- a/themes/bootstrap3/templates/RecordDriver/SolrDefault/core.phtml +++ b/themes/bootstrap3/templates/RecordDriver/SolrDefault/core.phtml @@ -55,8 +55,8 @@ ?> <? if (!empty($coreFields)): ?> <table class="table table-striped" summary="<?=$this->transEsc('Bibliographic Details')?>"> - <? foreach ($coreFields as $key => $value): ?> - <tr><th><?=$this->transEsc($key)?>:</th><td><?=$value?></td></tr> + <? foreach ($coreFields as $key => $current): ?> + <tr><th><?=$this->transEsc($key)?>:</th><td><?=$current['value']?></td></tr> <? endforeach; ?> </table> <? endif; ?> diff --git a/themes/bootstrap3/templates/RecordDriver/SolrDefault/data-authors.phtml b/themes/bootstrap3/templates/RecordDriver/SolrDefault/data-authors.phtml index d5e934703ba..bbc3a63584b 100644 --- a/themes/bootstrap3/templates/RecordDriver/SolrDefault/data-authors.phtml +++ b/themes/bootstrap3/templates/RecordDriver/SolrDefault/data-authors.phtml @@ -21,12 +21,14 @@ $formattedAuthors = []; <? // Display additional data using the appropriate translation prefix // (for example, to render author roles correctly): - foreach ($requiredDataFields as $field) { - $name = $field['name']; - $prefix = isset($field['prefix']) ? $field['prefix'] : ''; - if (isset($dataFields[$name])) { - echo $formatProperty($dataFields[$name], $name, $prefix); - } + if (!empty($requiredDataFields)) { + foreach ($requiredDataFields as $field) { + $name = $field['name']; + $prefix = isset($field['prefix']) ? $field['prefix'] : ''; + if (isset($dataFields[$name])) { + echo $formatProperty($dataFields[$name], $name, $prefix); + } + } } ?> </span> diff --git a/themes/bootstrap3/templates/RecordTab/description.phtml b/themes/bootstrap3/templates/RecordTab/description.phtml index e989574ff21..3d73373ffc5 100644 --- a/themes/bootstrap3/templates/RecordTab/description.phtml +++ b/themes/bootstrap3/templates/RecordTab/description.phtml @@ -7,8 +7,8 @@ ?> <table class="table table-striped" summary="<?=$this->transEsc('Description')?>"> <? if (!empty($mainFields)): ?> - <? foreach ($mainFields as $key => $value): ?> - <tr><th><?=$this->transEsc($key)?>:</th><td><?=$value?></td></tr> + <? foreach ($mainFields as $key => $current): ?> + <tr><th><?=$this->transEsc($key)?>:</th><td><?=$current['value']?></td></tr> <? endforeach; ?> <? else: ?> <tr><td><?=$this->transEsc('no_description')?></td></tr> -- GitLab