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.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:howtos:displaying_a_custom_field [2015/12/11 17:02] – ↷ Page moved from vufind2:displaying_a_custom_field to development:howtos:displaying_a_custom_field demiankatz | development:howtos:displaying_a_custom_field [2020/09/22 13:27] (current) – demiankatz | ||
---|---|---|---|
Line 7: | Line 7: | ||
===== Understanding VuFind Displays ===== | ===== Understanding VuFind Displays ===== | ||
- | All of VuFind' | + | All of VuFind' |
- | 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 [[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: |
- | For examples of display templates, see the [[https:// | + | For examples of display templates, see the [[https:// |
- | For examples of Solr-based retrievals, see the [[https:// | + | For examples of Solr-based retrievals, see the [[https:// |
===== Workflow for Adding a Field ===== | ===== Workflow for Adding a Field ===== | ||
Line 27: | Line 27: | ||
It is possible that the data is already available to VuFind. If you are retrieving a value from a field within the stored MARC record, or if you wish to display a field that is already visible elsewhere in VuFind, you can skip this step. | It is possible that the data is already available to VuFind. If you are retrieving a value from a field within the stored MARC record, or if you wish to display a field that is already visible elsewhere in VuFind, you can skip this step. | ||
- | If you need to process data at the indexing stage rather than loading it raw from MARC, you will need to adjust your [[..:SolrMarc]] configuration and/or your Solr schema.xml definition. See the discussion on [[..:adding facets]] for details on how to accomplish this. | + | If you need to process data at the indexing stage rather than loading it raw from MARC, you will need to adjust your [[indexing:solrmarc|SolrMarc]] configuration and/or your Solr schema.xml definition. See the discussion on [[indexing:adding_facets|adding facets]] for details on how to accomplish this. |
Don't forget to reindex your data and restart Solr after making changes like this! | Don't forget to reindex your data and restart Solr after making changes like this! | ||
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:// | + | 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:// |
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. | ||
- | Also note that while you can simply edit the existing record drivers in place, it is generally a better practice to localize your changes within a custom module to ease future upgrades. | + | Also note that while you can simply edit the existing record drivers in place, it is generally a better practice to localize your changes within a custom module to ease future upgrades. |
===== 3. Display the data in the appropriate template ===== | ===== 3. Display the data in the appropriate 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: | ||
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, | + | As with code customizations, |
- | ===== 4. Step-by-step | + | ===== 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 | + | |
- | + | ||
- | 1. Create a custom module (if you haven' | + | |
- | 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 / | + | |
- | + | ||
- | 2. From $VUFIND_HOME enter the following code generator command: | + | |
- | php public/ | + | |
- | + | ||
- | * You should see the following: | + | |
- | + | ||
- | Saved file: / | + | |
- | Saved file: / | + | |
- | | + | |
- | + | ||
- | * If you get an error like "Class ModuleName\RecordDriver\Factory does not exist", | + | |
- | + | ||
- | | + | |
- | php public/ | + | |
- | + | ||
- | 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 / | + | |
- | + | ||
- | | + | |
- | <? | + | |
- | | + | |
- | class SolrMarc extends \VuFind\RecordDriver\SolrMarc | + | |
- | { | + | |
- | /** | + | |
- | * Get the record ID of the current record. | + | |
- | * | + | |
- | * @return string | + | |
- | */ | + | |
- | public function getRecordID() { | + | |
- | return isset($this-> | + | |
- | | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | 4. Customize the template where you want to use that field by copying the appropriate files into your custom theme. | + | |
- | cd $VUFIND_HOME/ | + | |
- | mkdir ThemeName | + | |
- | + | ||
- | * Make the following sub-directory structure: ThemeName/ | + | |
- | * Put the following content in $VUFIND_HOME/ | + | |
- | + | ||
- | < | + | |
- | <?php | + | |
- | return array( | + | |
- | ' | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | * cd $VUFIND_HOME/ | + | |
- | * Edit core.phtml and result-list.phtml to add new fields for display. | + | |
- | + | ||
- | Example: | + | |
- | <? $recordID | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | <? endif; ?> | + | |
- | + | ||
- | * Edit local/ | + | |
+ | * [[development: | ||
+ | * [[development: | ||
---- struct data ---- | ---- struct data ---- | ||
+ | properties.Page Owner : | ||
---- | ---- | ||
development/howtos/displaying_a_custom_field.1449853357.txt.gz · Last modified: 2015/12/11 17:02 by demiankatz