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

Differences

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

Link to this comparison view

Next revision
Previous revision
development:howtos:displaying_a_custom_field:vufind6_example [2020/04/14 12:39] – created demiankatzdevelopment:howtos:displaying_a_custom_field:vufind6_example [2023/11/09 21:32] (current) demiankatz
Line 1: Line 1:
-====== Displaying a Custom ThemeVuFind 6.x Example ======+====== Displaying a Custom FieldVuFind® 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. // // 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. //
Line 51: Line 51:
    cd $VUFIND_HOME/themes    cd $VUFIND_HOME/themes
    mkdir -p ThemeName/templates/RecordDriver/SolrMarc/    mkdir -p ThemeName/templates/RecordDriver/SolrMarc/
 +
   * Put the following content in $VUFIND_HOME/themes/ThemeName/theme.config.php:   * Put the following content in $VUFIND_HOME/themes/ThemeName/theme.config.php:
  
Line 63: Line 64:
   * Edit $VUFIND_HOME/themes/ThemeName/templates/RecordDriver/SolrMarc/result-list.phtml to add the new field for display.   * 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": +For example, you could add this immediately preceding the div with a class of "callnumAndLocation": 
-      <? $recordID = $this->driver->getRecordID(); if (!empty($recordID)): ?> + 
-      <div> +   <? $recordID = $this->driver->getRecordID(); if (!empty($recordID)): ?> 
-        <b><?=$this->transEsc('Record ID')?>:</b> +     <div> 
-        <?=$this->escapeHtml($recordID)?> +       <b><?=$this->transEsc('Record ID')?>:</b> 
-       </div> +       <?=$this->escapeHtml($recordID)?> 
-      <? endif; ?>+     </div> 
 +   <? endif; ?>
  
   * Edit $VUFIND_LOCAL_DIR/config/vufind/config.ini to set theme = ThemeName   * 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:
 +
 +   <?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.
 ---- struct data ---- ---- struct data ----
 +properties.Page Owner : 
 ---- ----
  
development/howtos/displaying_a_custom_field/vufind6_example.1586867991.txt.gz · Last modified: 2020/04/14 12:39 by demiankatz