This is an old revision of the document!
Table of Contents
RecordDataFormatter view helper
This page describes functionality that was added in VuFind 4.0.
VuFind includes a RecordDataFormatter view helper designed to make it easier to customize the contents of tabular displays of data generated from record driver objects.
The RecordDataFormatter's most important public method is getData(), which accepts two things as input:
- A specification, in the form of an associative array, describing the fields of the output table and how to obtain the necessary data to populate them.
- A record driver object.
As output, getData() returns an associative array of field name => HTML output for the corresponding field. Field names are raw and should be translated/escaped when displayed in templates.
Additionally, the helper includes getDefaults($key) / setDefaults($key) methods which can be used to store or retrieve default specification arrays. VuFind ships with stored specifications for 'collection-info', 'collection-record', 'core' and 'description', which are used in the corresponding .phtml files in the record driver template directory. See below on how to change/customize these defaults.
The specification array uses as keys the labels to be displayed in the resulting table of data. Each key is associated with a value which is itself an associative array with one or more of the following keys:
|allowZero||optional (default = true)||If the raw data evaluates to 0 or '0', should we display it (true) or suppress the whole field (false)?|
|context||optional||When template rendering is used, an array of values to pass to the template, when renderType = RecordDriverTemplate.|
|dataMethod||required||The record driver method to use for extracting raw field data used for generating output. May be set to boolean true to always generate output without variable input; may be set to boolean false to suppress the entire field.|
|helperMethod||optional||The Record view helper method to use for output rendering, when renderType = RecordHelper.|
|pos||optional||An integer used for sorting the fields to determine the final display order.|
|renderType||optional (default = Simple)||The method used to render output from the input provided by dataMethod. Legal values: RecordDriverTemplate (render a template associated with the record driver, as specified by the 'template' key); RecordHelper (call a method of the Record view helper, as specified by the 'method' key); Simple (display the raw data, possibly manipulated based on other settings in the specification)|
|labelFunction||optional||May be set to a callback function which will receive the raw field data as input and produce a custom output label (overriding the key of the spec array) as output.|
|prefix||optional||HTML to prepend to the output generated by the view helper. Applies only when RenderType = Simple.|
|recordLink||optional||The type of link (from the Record view helper's getLink method) to associate with each value in the raw field data. Applies only when RenderType = Simple.|
|separator||optional (default = <br />)||Separator to use between multiple data values. Applies only when RenderType = Simple.|
|suffix||optional||HTML to append to the output generated by the view helper. Applies only when RenderType = Simple.|
|template||optional||The name of the template to render, when renderType = RecordDriverTemplate|
|translate||optional (default = false)||Should we run raw field data through the translator? Applies only when RenderType = Simple.|
|useCache||optional (default = false)||Should we cache the raw field data in the view helper to avoid duplicate calls?|
The Specification Builder
A SpecBuilder convenience class has been provided to help generate specification arrays. This handles such functionality as auto-generating useful 'pos' values, reordering/overriding fields, etc.
Useful public methods:
- setLine($key, $dataMethod, $renderType = null, $options = ) - add a configuration for $key; defaults to 'Simple' $renderType
- setTemplateLine($key, $dataMethod, $template, $options = ) - add a template-based configuration for $key; convenience method wrapped around setLine()
- reorderKeys($orderedKeys, $defaultPos = null) - reorder the items within the configuration based on an ordered array of key names
There are two simple ways to change specifications.
Option 1: Override the Factory
If you want to make global changes to a specification, the easiest solution is to extend \VuFind\View\Helper\Root\RecordDataFormatterFactory in your own custom module and override getDefaultCoreSpecs() and/or getDefaultDescriptionSpecs() as needed. You can either completely rewrite the spec generation logic, or you can call the parent version of the function and then manipulate the array slightly to add, remove or reorder fields.
Option 2: Override the Template
If you want to change behavior for a specific record driver, or if you prefer to keep your code changes at a higher level, you can also customize the appropriate record driver template(s), simply manipulating the spec array between the call to getDefaults() and the call to getData().