====== Displaying a Custom Field: VuFind® 6.x (or newer) Example ====== // This example shows you how to set up a custom module, custom theme and custom record driver. It assumes that you have already added a custom field to your Solr index named RecID -- you can see more concrete examples of adding fields on the [[indexing:adding_facets|Adding Facets]] page. // 1. Create a custom module (if you haven't already done so) cd $VUFIND_HOME php install.php * At the prompt "What module name would you like to use? [blank for none]" type in your module name * Link configuration to apache’s config.d directory per the install.php directions * Restart apache (sudo systemctl restart apache2, or whatever equivalent command is appropriate in your environment) 2. Make sure that your module name is exported in an environment variable so that generator commands work correctly: export VUFIND_LOCAL_MODULES=ModuleName 3. From $VUFIND_HOME enter the following code generator command: php public/index.php generate extendclass VuFind\\RecordDriver\\SolrMarc ModuleName * You should see something similar to the following: Saved file: /home/dkatz/vufind3/module/ModuleName/src/ModuleName/RecordDriver/SolrMarc.php Created backup: /home/dkatz/vufind3/module/ModuleName/config/module.config.php.1586866995.bak Successfully updated /home/dkatz/vufind3/module/ModuleName/config/module.config.php Successfully updated /home/dkatz/vufind3/module/ModuleName/config/module.config.php Successfully updated /home/dkatz/vufind3/module/ModuleName/config/module.config.php 4. Update the $VUFIND_HOME/module/ModuleName/src/ModuleName/RecordDriver/SolrMarc.php file to add a new field. * Make sure the namespace line reads “namespace ModuleName\RecordDriver;” * Add the getter method. See below for an example, but also reference the SolrMarc.php, MarcBasicTrait.php and MarcAdvancedTrait.php files in /module/VuFind/src/VuFind/RecordDriver/ for more examples of getter code. Example: fields['recID'] ?? ''; } } 5. To add the new field to a result list, you will need to set up a custom theme (if you don't already have one) and copy the appropriate files into it. * First, set up the necessary directory structure: cd $VUFIND_HOME/themes mkdir -p ThemeName/templates/RecordDriver/SolrMarc/ * Put the following content in $VUFIND_HOME/themes/ThemeName/theme.config.php: 'bootstrap3' ]; * Copy $VUFIND_HOME/themes/bootstrap3/templates/RecordDriver/DefaultRecord/result-list.phtml into your new theme’s SolrMarc directory (e.g. cp $VUFIND_HOME/themes/bootstrap3/templates/RecordDriver/DefaultRecord/result-list.phtml $VUFIND_HOME/themes/ThemeName/templates/RecordDriver/SolrMarc/). Note that copying the file into the SolrMarc directory will ONLY override the template for MARC records. If you wanted to override it for every type of record, you would instead copy it into the custom theme's DefaultRecord directory. * Edit $VUFIND_HOME/themes/ThemeName/templates/RecordDriver/SolrMarc/result-list.phtml to add the new field for display. For example, you could add this immediately preceding the div with a class of "callnumAndLocation": driver->getRecordID(); if (!empty($recordID)): ?>
transEsc('Record ID')?>: escapeHtml($recordID)?>
* Edit $VUFIND_LOCAL_DIR/config/vufind/config.ini to set theme = ThemeName 6. To add a new field to the core record view, you will need to add some custom configuration to the [[development:architecture:record_data_formatter|record data formatter]]. * First, create this directory: mkdir -p $VUFIND_HOME/module/ModuleName/src/ModuleName/View/Helper/Root * Now, create a custom RecordDataFormatterFactory in your local code module; this is where configurations are set up. For example, you could edit $VUFIND_HOME/module/ModuleName/src/ModuleName/View/Helper/Root/RecordDataFormatterFactory.php and add this code: setLine('Record ID', 'getRecordID'); return $spec->getArray(); } } * Finally, edit $VUFIND_HOME/themes/ThemeName/theme.config.php and adjust it to look like this: 'bootstrap3', 'helpers' => ['factories' => ['VuFind\View\Helper\Root\RecordDataFormatter' => 'ModuleName\View\Helper\Root\RecordDataFormatterFactory']], ]; :!: In this example, the key line is $spec->setLine('Record ID', 'getRecordID'); in your custom RecordDataFormatterFactory. This tells VuFind® to call the record driver's getRecordID() method and display the value(s) with a label of "Record ID." This is an easy way to add a simple label-value pair to your display. If you instead needed to display the value in a more complex way requiring a custom template, you could instead say $spec->setTemplateLine('Record ID', 'getRecordID', 'data-recordid.phtml') and then create a $VUFIND_HOME/themes/ThemeName/templates/RecordDriver/SolrMarc/data-recordid.phtml template containing appropriate formatting logic. The value(s) from getRecordID will be available in the template as a PHP variable named $data.