About Features Downloads Getting Started Documentation Events Support GitHub

Site Tools


Warning: This page has not been updated in over over a year and may be outdated or deprecated.
development:architecture:record_data_formatter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
development:architecture:record_data_formatter [2016/12/01 15:51] – [Specification Array] demiankatzdevelopment:architecture:record_data_formatter [2020/02/27 14:08] – [Specification Array] demiankatz
Line 1: Line 1:
 ====== RecordDataFormatter view helper ====== ====== 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 [[development:plugins:record_drivers|record driver]] objects. VuFind includes a RecordDataFormatter view helper designed to make it easier to customize the contents of tabular displays of data generated from [[development:plugins:record_drivers|record driver]] objects.
Line 10: Line 12:
   * A [[development:plugins:record_drivers|record driver]] object.   * A [[development:plugins:record_drivers|record driver]] object.
  
-As output, getData() returns an associative array of field name <nowiki>=></nowiki> HTML output for the corresponding field. Field names are raw and should be translated/escaped when displayed in templates.+As output, getData() returns different formats depending on your VuFind version. In 4.x, it is an associative array of field name <nowiki>=></nowiki> another associative array containing HTML output ('value' key) and contextual information ('context' key) for the corresponding field. :!: Starting with 5.0, the return format changes to a non-associative array of arrays, with each sub-array containing a 'label' key for the field name.
  
-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 '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.+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.
  
 ===== Specification Array ===== ===== Specification Array =====
Line 20: Line 24:
 ^Key^Optional/Required^Description^ ^Key^Optional/Required^Description^
 | allowZero | optional (default = true) | If the raw data evaluates to 0 or '0', should we display it (true) or suppress the whole field (false)? | | 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. |+| context | optional | An array of contextual values that may be used to help with template rendering; these are always passed through as the 'context' key of the output of the view helper. When template rendering (renderType = RecordDriverTemplate) is used, these values are also passed directly to the target template |
 | 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. | | 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. |
-method | optional | The Record view helper method to use for output rendering, when renderType = RecordHelper. |+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. | | 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) | +| renderType | optional (default = Simple) | The method used to render output from the input provided by dataMethod. Legal values: **Multi** (sort data using a callback function specified by the 'multiFunction' key and potentially render each output group using different rules), **RecordDriverTemplate** (render a template associated with the record driver, as specified by the 'template' key); **RecordHelpe**r (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. | +| 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. Starting with VuFind 5.0, the callback function also receives the record driver object as a second parameter. | 
-| prefix | optional | HTML to prepend to the output generated by the view helper. Applies only when RenderType = Simple. |+| multiFunction | optional | The callback required when renderType = Multi. Accepts three parameters: 1.) raw input data, 2.) the options array from the spec, 3.) the record driver object. Outputs an array of associative arrays with the following keys: **label**: Label to display; **values**: Values to render; **options**: Rendering options (may include renderType, pos, and any other relevant parameters; they will be applied only to the current grouping of data; any options not set here will be inherited from the spec array). | 
 +| multiRenderType | optional | The default type of rendering to apply to each of the sets of values returned by the multiFunction (optional, defaults to Simple, may be overridden by options specified by your multiFunction). | 
 +| itemPrefix | optional | :!: Introduced in VuFind 7.0. HTML to prepend to each value formatted by the view helper. Use prefix instead if you want to prepend a single value to the whole set of data assembled by the view helper. Applies only when RenderType = Simple. | 
 +| prefix | optional | HTML to prepend to the output generated by the view helper. This is applied once before the full set of values; use itemPrefix if you want to prepend something to each individual value. 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. | | 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 = <nowiki><br /></nowiki>) | Separator to use between multiple data values. Applies only when RenderType = Simple. | | separator | optional (default = <nowiki><br /></nowiki>) | 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. |+| itemSuffix | optional | :!: Introduced in VuFind 7.0. HTML to append to each value formatted by the view helper. Use suffix instead if you want to append a single value to the whole set of data assembled by the view helper. Applies only when RenderType = Simple. | 
 +| suffix | optional | HTML to append to the output generated by the view helper. This is applied once after the full set of values; use itemSuffix if you want to append something to each individual value. Applies only when RenderType = Simple. |
 | template | optional | The name of the template to render, when renderType = RecordDriverTemplate | | 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. | | translate | optional (default = false) | Should we run raw field data through the translator? Applies only when RenderType = Simple. |
 +| translationTextDomain | optional (default = '') | Prefix to apply to value string prior to running text through the translator; most useful for applying a text domain, but may be used for other purposes (for example, 'CreatorRole::' or 'location_'). Applies only when RenderType = Simple. :!: // Introduced in VuFind 4.1. // |
 | useCache | optional (default = false) | Should we cache the raw field data in the view helper to avoid duplicate calls? | | useCache | optional (default = false) | Should we cache the raw field data in the view helper to avoid duplicate calls? |
  
 +===== The Specification Builder =====
 +
 +A [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter/SpecBuilder.php|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
 +  * setMultiLine($key, $dataMethod, $callback, $options = []) - add a multi-function-based configuration for $key; convenience method wrapped around setLine()
 +  * 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
 ===== Customizing Specifications ===== ===== Customizing Specifications =====
  
Line 41: Line 59:
 ==== Option 1: Override the Factory ==== ==== 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 [[development:architecture:customizing_vufind#modules|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.+If you want to make global changes to a specification, the easiest solution is to extend [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php|\VuFind\View\Helper\Root\RecordDataFormatterFactory]] in your own [[development:architecture:customizing_vufind#modules|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 ==== ==== Option 2: Override the Template ====
development/architecture/record_data_formatter.txt · Last modified: 2020/09/22 14:19 by demiankatz