About Features Downloads Getting Started Documentation Events Support GitHub

Love VuFind®? Consider becoming a financial supporter. Your support helps build a better VuFind®!

Site Tools


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

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
  • 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: 
 <?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.

  • 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:
<?php
return [
    'extends' => '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”:

 <? $recordID = $this->driver->getRecordID(); if (!empty($recordID)): ?>
   <div>
     <b><?=$this->transEsc('Record ID')?>:</b>
     <?=$this->escapeHtml($recordID)?>
   </div>
 <? endif; ?>
  • 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 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:
 <?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();
     }
 }
  • Finally, edit $VUFIND_HOME/themes/ThemeName/theme.config.php and adjust it to look like this:
 <?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.

development/howtos/displaying_a_custom_field/vufind6_example.txt · Last modified: 2023/11/09 21:32 by demiankatz