Reorder or omit select metadata fields

I had a client ask to reorder metadata elements, which of course can be done in the admin settings. However, if you need to, say, put an Item Type Metadata field before a Dublin Core field, you’ll need a different solution. You might also have a custom template that foregrounds certain elements and you don’t want to repeat them if you also need to dump the full metadata details elsewhere on the page. Anyway, I figured I’d share this in case it’s useful to others.

Custom Function

The function below allows you to display all metadata, while omitting specific Dublin Core or Item Type Metadata fields. Use this function instead of all_element_texts() in your items/show.php template.

function custom_all_metadata($record=null,$omitdc=array(),$omitit=array(),$headings=false, $html=null){
    $all = all_element_texts($record,array('return_type'=>'array'));
    $itemType = get_record_by_id('ItemType', $record->item_type_id);
    $typename = $itemType && isset($itemType['name']) ? $itemType['name'].' ' : null;
    $hasomissions=false;
    if(count($omitdc)){
        if(isset(($all['Dublin Core'])) && count($all['Dublin Core'])){
            $hasomissions=true;
            foreach($omitdc as $omit){
                if(isset($all['Dublin Core'][$omit])){
                    unset($all['Dublin Core'][$omit]);
                }
            }
        }
    }
    if(count($omitit)){
         if(isset(($all[$typename.'Item Type Metadata'])) && count($all[$typename.'Item Type Metadata'])){
             $hasomissions=true;
             foreach($omitit as $omit){
                 if(isset($all[$typename.'Item Type Metadata'][$omit])){
                     unset($all[$typename.'Item Type Metadata'][$omit]);
                 }
             }
         }
    }
    if(count($all) && ($hasomissions)){
        foreach($all as $set){
           foreach($set as $key=>$values){
               $html .= '<div class="element">';
               $html .= '<h3>'.$key.'</h3>';
               foreach($values as $value){
                   $html .= '<div class="element-text">'.$value.'</div>';
               }
               $html .= '</div>';
           }
        }
    }
    if($html){
        return '<div class="meta-container">'.$html.'</div>';
    }elseif(!$hasomissions){
        return all_element_texts($record, array('show_element_set_headings'=>$headings,'return_type'=>'html'));
    } 
}

Usage

Below is an example of how you might show specific elements before the full metadata display without repetition.

<?php
// description
if($description=metadata('item',array('Dublin Core','Description'))){
    echo '<div class="special">'.$description.'</div>';
}
// transcription
if($transcription=metadata('item',array('Item Type Metadata','Transcription'))){
    echo '<div class="special">'.$transcription.'</div>';
}
// everything else...
echo custom_all_metadata($record,array('Description'),array('Transcription'));
?>

Worth noting that Hide Elements does this using the regular all_element_texts (so, on stock themes).

The type of solution you’re using here makes sense as part of a customized theme so the alterations are all contained in the customized show view.

Yeah, this is definitely intended for someone who is creating a custom theme.