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:vufind3_example

Displaying a Custom Field: VuFind® 3.x 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 /etc/init.d/apache2 restart)

2. From $VUFIND_HOME enter the following code generator command:

 php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc ModuleName
  • You should see the following:
 Saved file: /usr/local/vufind/module/ModuleName/src/ModuleName/RecordDriver/SolrMarc.php
 Saved file: /usr/local/vufind/module/ModuleName/src/ModuleName/RecordDriver/Factory.php
 Successfully updated /usr/local/vufind/module/ModuleName/config/module.config.php
  • If you get an error like “Class ModuleName\RecordDriver\Factory does not exist”, or if the files do not get written, then do the following:
 export VUFIND_LOCAL_MODULES=ModuleName
 php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc ModuleName

3. Update the 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 file 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 isset($this->fields['recID']) ?
         $this->fields['recID'] : '';
    }
 }

4. Customize the template where you want to use that field by copying the appropriate files into your custom theme.

 cd $VUFIND_HOME/themes
 mkdir ThemeName
  • Make the following sub-directory structure: ThemeName/templates/RecordDriver/SolrDefault/
  • Put the following content in $VUFIND_HOME/themes/ThemeName/theme.config.php:
<?php
return array(
    'extends' => 'bootstrap3'
);
  • cd $VUFIND_HOME/themes/bootstrap3/templates/RecordDriver/SolrDefault/, and copy core.phtml and result-list.phtml to new theme’s SolrDefault directory
  • Edit core.phtml and result-list.phtml to add new fields for display.
  Example:
    <? $recordID = $this->driver->getRecordID(); if (!empty($recordID)): ?>
    <tr>
      <th><?=$this->transEsc('Record ID')?>: </th>
       <td property="RecID"><?=$this->escapeHtml($recordID)?></td>
     </tr>
    <? endif; ?>
  • Edit local/config/vufind/config.ini to set theme = ThemeName
development/howtos/displaying_a_custom_field/vufind3_example.txt · Last modified: 2023/11/09 21:33 by demiankatz