diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php index e07720c86737c6a4f57afbc58194bd3b5f6b7e7c..b186f4f2eb44676a028fd02bfbced610470d7899 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php @@ -121,19 +121,35 @@ class RecordDataFormatter extends AbstractHelper */ public function getDefaults($key) { - return isset($this->defaults[$key]) ? $this->defaults[$key] : []; + // No value stored? Return empty array: + if (!isset($this->defaults[$key])) { + return []; + } + // Callback stored? Resolve to array on demand: + if (is_callable($this->defaults[$key])) { + $this->defaults[$key] = $this->defaults[$key](); + if (!is_array($this->defaults[$key])) { + throw new \Exception('Callback for ' . $key . ' must return array'); + } + } + // Send back array: + return $this->defaults[$key]; } /** * Set default configuration. * - * @param string $key Key for configuration to set. - * @param array $values Defaults to store. + * @param string $key Key for configuration to set. + * @param array|Callable $values Defaults to store (either an array, or a + * Callable returning an array). * * @return void */ - public function setDefaults($key, array $values) + public function setDefaults($key, $values) { + if (!is_array($values) && !is_callable($values)) { + throw new \Exception('$values must be array or Callable'); + } $this->defaults[$key] = $values; } diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php index 478638a654b142b9efd9a68ecbf87d028ecc12b1..e03388acb4dbb8e7d591f5fbb2d414a0e3d6f88f 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php @@ -49,12 +49,12 @@ class RecordDataFormatterFactory { $helper = new RecordDataFormatter(); $helper - ->setDefaults('collection-info', $this->getDefaultCollectionInfoSpecs()); + ->setDefaults('collection-info', [$this, 'getDefaultCollectionInfoSpecs']); $helper->setDefaults( - 'collection-record', $this->getDefaultCollectionRecordSpecs() + 'collection-record', [$this, 'getDefaultCollectionRecordSpecs'] ); - $helper->setDefaults('core', $this->getDefaultCoreSpecs()); - $helper->setDefaults('description', $this->getDefaultDescriptionSpecs()); + $helper->setDefaults('core', [$this, 'getDefaultCoreSpecs']); + $helper->setDefaults('description', [$this, 'getDefaultDescriptionSpecs']); return $helper; }