Gallery2:How to display custom fields next to thumbnail - Gallery Codex
Personal tools

Gallery2:How to display custom fields next to thumbnail

From Gallery Codex

Gallery offers a theme setting that enables the display of an individual album's or item's custom field block. However, if you'd like to display an item's custom fields next to its thumbnail in an album, try one of the following theme modifications.

Custom Field Template Callback

If you only need to display a few custom fields you may use the customfield.LoadCustomFields callback. This method requires a simple edit to your theme's album.tpl file.

Important: If you're editing one of Gallery's stock themes, review How to Edit an Existing Theme.

  1. Open the album template file for your theme (ex. themes/matrix/templates/album.tpl)
  2. Locate where item thumbnails, titles, and descriptions are displayed in the album.tpl (look for $child.title).
  3. Copy and paste the following near the thumbnail, title, or description
 {* Use the customfield.LoadCustomFields callback to get an array of fields *}
 {g->callback type="customfield.LoadCustomFields" itemId=$child.id}
 {if !empty($block.customfield.LoadCustomFields.fields.Item)}
   <ul>
     {foreach from=$block.customfield.LoadCustomFields.fields item=customField}
     <li>{$customField}</li>
     {/foreach}
   </ul>
 {/if}

After saving album.tpl, you should now see all available custom fields next to items in your album pages. Callbacks are easy to implement and work well. If you have a high-volume web site and performance is a concern, load custom field data in your theme's theme.inc file.

Load Custom Field Data in Theme.inc

High-traffic sites that display a large number of items will benefit by loading custom fields in theme.inc instead of using a callback.

  1. Open the theme's theme.inc file (ex. themes/matrix/theme.inc)
  2. Add the following within the showAlbumPage() function, just above the functions return line, return array(null, 'theme.tpl').
 /* Get custom fields */
 list ($ret, $children) = GalleryCoreApi::loadEntitiesById($childIds);
 if ($ret) {
   return array($ret, null);
 }
 
 list ($ret, $theme['customFieldValues']) =
   CustomFieldHelper::fetchFieldValues($children, 'detail');
 if ($ret) {
   return array($ret, null);
 }

Next, display custom fields in album.tpl

  1. Open the album template file for your theme (ex. themes/matrix/templates/album.tpl)
  2. Locate where item thumbnails, titles, and descriptions are displayed in the album.tpl (look for $child.title)
  3. Copy and paste the following near the thumbnail, title, or description
 {* Show a child's custom fields *}
 {if isset($theme.customFieldValues[$child.id])}
 <ul>
   {foreach from=$theme.customFieldValues[$child.id] key=name item=value}
   <li>{$name}: {$value}</li>
   {/foreach}
 </ul>
 {/if}