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 Adding Facets page.

1. Create a custom module (if you haven't already done so)

 cd $VUFIND_HOME
 php install.php

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
 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.

 Example: 
 <?php
 namespace ModuleName\RecordDriver;
 class SolrMarc extends \VuFind\RecordDriver\SolrMarc
 {
     /**
      * Get the record ID of the current record.
      *
      * @return string
      */
     public function getRecordID()
     {
         return $this->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.

 cd $VUFIND_HOME/themes
 mkdir -p ThemeName/templates/RecordDriver/SolrMarc/
<?php
return [
    'extends' => 'bootstrap3'
];

For example, you could add this immediately preceding the div with a class of “callnumAndLocation”:

 <? $recordID = $this->driver->getRecordID(); if (!empty($recordID)): ?>
   <div>
     <b><?=$this->transEsc('Record ID')?>:</b>
     <?=$this->escapeHtml($recordID)?>
   </div>
 <? endif; ?>

6. To add a new field to the core record view, you will need to add some custom configuration to the record data formatter.

 mkdir -p $VUFIND_HOME/module/ModuleName/src/ModuleName/View/Helper/Root
 <?php
 
 namespace ModuleName\View\Helper\Root;
 
 use VuFind\View\Helper\Root\RecordDataFormatter\SpecBuilder;
 
 class RecordDataFormatterFactory extends \VuFind\View\Helper\Root\RecordDataFormatterFactory
 {
     public function getDefaultCoreSpecs()
     {
         $spec = new SpecBuilder(parent::getDefaultCoreSpecs());
         $spec->setLine('Record ID', 'getRecordID');
         return $spec->getArray();
     }
 }
 <?php
 return [
     'extends' => '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.