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

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:howtos:displaying_a_custom_field [2020/04/14 12:04] – [4. Step-by-step example] demiankatzdevelopment:howtos:displaying_a_custom_field [2020/09/22 13:27] (current) demiankatz
Line 11: Line 11:
 In the case of Solr, stored field data from the index is made available to the record driver through its fields property; in the case of MARC, convenience methods are provided for extracting data. For other data formats and sources, availability and format of internal data will vary according to the design of the [[development:howtos:connecting_a_new_external_data_source|search backend]]. In the case of Solr, stored field data from the index is made available to the record driver through its fields property; in the case of MARC, convenience methods are provided for extracting data. For other data formats and sources, availability and format of internal data will vary according to the design of the [[development:howtos:connecting_a_new_external_data_source|search backend]].
  
-For examples of display templates, see the [[https://github.com/vufind-org/vufind/tree/master/themes/bootstrap3/templates/RecordDriver/DefaultRecord|DefaultRecord]] templates within the Bootstrap3 theme. An understanding of the [[https://vufind.org/wiki/development:architecture:record_data_formatter|record data formatter]] will also help clarify how these work.+For examples of display templates, see the [[https://github.com/vufind-org/vufind/tree/dev/themes/bootstrap3/templates/RecordDriver/DefaultRecord|DefaultRecord]] templates within the Bootstrap3 theme. An understanding of the [[development:architecture:record_data_formatter|record data formatter]] will also help clarify how these work.
  
-For examples of Solr-based retrievals, see the [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php|DefaultRecord]] driver; for examples of MARC-based retrievals, see the [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php|SolrMarc]] driver (and its [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php|MarcAdvancedTrait]]).+For examples of Solr-based retrievals, see the [[https://github.com/vufind-org/vufind/blob/dev/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php|DefaultRecord]] driver; for examples of MARC-based retrievals, see the [[https://github.com/vufind-org/vufind/blob/dev/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php|SolrMarc]] driver (and its [[https://github.com/vufind-org/vufind/blob/dev/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php|MarcAdvancedTrait]]).
  
 ===== Workflow for Adding a Field ===== ===== Workflow for Adding a Field =====
Line 34: Line 34:
 If you are displaying a field that is already defined within VuFind, you can skip this step. If you are displaying a field that is already defined within VuFind, you can skip this step.
  
-If you are adding a totally new field, you will need to add an appropriate getter method to the record driver used to display your records. In the most common situation, this is going to be the [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php|SolrMarc]] driver, which inherits most of its functionality from the [[https://github.com/vufind-org/vufind/blob/master/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php|SolrDefault]] driver. The existing get methods in these classes should serve as useful examples for creating new methods.+If you are adding a totally new field, you will need to add an appropriate getter method to the record driver used to display your records. In the most common situation, this is going to be the [[https://github.com/vufind-org/vufind/blob/dev/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php|SolrMarc]] driver, which inherits most of its functionality from the [[https://github.com/vufind-org/vufind/blob/dev/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php|SolrDefault]] driver. The existing get methods in these classes should serve as useful examples for creating new methods.
  
 If you copy an existing method as a template for your new method, the most important detail to watch out for is the return value: be sure that you return a string when dealing with a single-valued Solr field and that you return an array when dealing with a multi-valued Solr field. This will make it easier for you to deal with data cleanly in your template. If you copy an existing method as a template for your new method, the most important detail to watch out for is the return value: be sure that you return a string when dealing with a single-valued Solr field and that you return an array when dealing with a multi-valued Solr field. This will make it easier for you to deal with data cleanly in your template.
Line 45: Line 45:
 Some key templates that you are likely to consider editing: Some key templates that you are likely to consider editing:
  
-  * core.phtml - the metadata displayed on the record view, regardless of the active tab 
   * result-grid.phtml - a single search result, when "grid view" is active   * result-grid.phtml - a single search result, when "grid view" is active
   * result-list.phtml - a single search result, when "list view" (the default mode) is active   * result-list.phtml - a single search result, when "list view" (the default mode) is active
 +  * core.phtml - the metadata displayed on the record view, regardless of the active tab (though in VuFind 4.x and newer, it is easier to change this by reconfiguring the [[development:architecture:record_data_formatter|record data formatter]] rather than by overriding the template)
  
 It should be possible to add the new field to the template by imitating some of the other code already present there. It should be possible to add the new field to the template by imitating some of the other code already present there.
  
 As with code customizations, it is possible to edit templates in-place, but it is often better practice to create your own custom theme and create your custom views there -- this will make future upgrades easier. See [[development:architecture:customizing_vufind|Customizing VuFind]] for more details. As with code customizations, it is possible to edit templates in-place, but it is often better practice to create your own custom theme and create your custom views there -- this will make future upgrades easier. See [[development:architecture:customizing_vufind|Customizing VuFind]] for more details.
-===== 4. Step-by-step example (VuFind 3.x) ===== +===== 4. Step-by-step examples =====
- +
-// 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 /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: +
- +
-<code> +
-<?php +
-return array( +
-    'extends' => 'bootstrap3' +
-); +
-</code> +
- +
-  * 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; ?> +
- +
-:!: Note that, starting with VuFind 4.0, the [[development:architecture:record_data_formatter|record data formatter]] is introduced to simplify the display templates; the example above applies to earlier versions, and adding a new field to displays requires slightly different changes in later versions. See [[https://vufind.org/wiki/development:architecture:record_data_formatter|this wiki page]] for more details. +
- +
-  * Edit local/config/vufind/config.ini to set theme ThemeName+
  
 +  * [[development:howtos:displaying_a_custom_field:vufind6_example|Example for VuFind 6.x or newer]]
 +  * [[development:howtos:displaying_a_custom_field:vufind3_example|Example for VuFind 3.x]]
 ---- struct data ---- ---- struct data ----
 +properties.Page Owner : 
 ---- ----
  
development/howtos/displaying_a_custom_field.1586865849.txt.gz · Last modified: 2020/04/14 12:04 (external edit)