Print meta data tags in header.php

Hi Omeka community,

I am having trouble trying to print out meta data fields eg in the header template.

I can easily hardcode the meta data tag however I am not sure of the variable or function to use to retrieve the meta data information for things such as dublin core handle ID, dublin core title, dublin core creator etc in the header. I tried using the $element_text = all_element_texts(‘item’); from the which appeared to work, however this would then create an error exception for the Browse By page.

Is there an alternative variable or function to use in the header or a way to prevent it breaking the Browse By page?

Many thanks,
Jenny

It sounds like you need to check whether there is an item set for the page. Then, use the metadata function to get the data you want.

Just something like this should do the trick


if (isset($item) {
   $title = metadata($item, array('Dublin Core', 'Title');
// etc for the other metadata, and echo it out
}

Hi Patrick,

I tried that and a few other functions such as isset(), is_null() and empty() but unfortunately when there is no item parsed, it throws a fatal error. The same happens if I just do a straight print of this line: metadata(‘item’, array(‘Dublin Core’, ‘Identifier’)))

I’m running out of ideas. Any more clues would be greatly appreciated!

You can improve the code above to check collection, or file or record.

$record = isset($record) ? $record : (isset($item) ? $item : (isset($collection) ? $collection : (isset($file) ? $file : (isset($items) ? reset($items) : (isset($collections) ? reset($collections) : null)))));
if ($record) {
    $title = metadata($record, array('Dublin Core', 'Title'));
    // etc for the other metadata, and echo it out
}

@jsoohoo could you post up the code you are using, and the details of the fatal error?

Thank you for your help!

@Daniel_KM it appears the header.php does not pass anything in the $record which seems odd, as it continues to the else statement when I test it on an item page. There is no error exception in the Browse By page when I use this code snippet which is positive.

$record = isset($record) ? $record : (isset($item) ? $item : (isset($collection) ? $collection : (isset($file) ? $file : (isset($items) ? reset($items) : (isset($collections) ? reset($collections) : null)))));
print_r($record);
if ($record) {
$title = metadata($record, array(‘Dublin Core’, ‘Identifier’));
$identifier = metadata($record, array(‘Dublin Core’, ‘Identifier’));
echo $identifier;
// etc for the other metadata, and echo it out
}
else {
echo “

”;
}

@patrickmj When I don’t have a conditional statement to capture whether a record, item or collection is being parsed, and I purely print out $identifier = metadata($record, array(‘Dublin Core’, ‘Identifier’)); it will throw the following error on the Browse By page (I took two screenshots to show the full error message).

In the simple pages, and for exhibits and for pages of specific plugins, there may be no record, so :

if ($record) {
    $title = metadata($record, array('Dublin Core', 'Identifier'));
    $identifier = metadata($record, array('Dublin Core', 'Identifier'));
    // etc for the other metadata, and echo it out
}
elseif (isset($simple_pages_page)) {
    $title = metadata($simple_pages_page, 'title');
    $identifier = metadata($simple_pages_page, 'slug');
}

Thanks @Daniel_KM,

I have used the code snippet in the header.php but it still does not seem to print anything but the else statement. To confirm, I am editing the /omeka/web/default/themes/default/common/header.php file. If it helps, I test the page on the front end by viewing source on record item: https://repository.monash.edu/items/show/39548

$record = isset($record) ? $record : (isset($item) ? $item : (isset($collection) ? $collection : (isset($file) ? $file : (isset($items) ? reset($items) : (isset($collections) ? reset($collections) : null)))));

if ($record) {
$title = metadata($record, array('Dublin Core', 'Identifier'));
$identifier = metadata($record, array('Dublin Core', 'Identifier'));
// etc for the other metadata, and echo it out
}
elseif (isset($simple_pages_page)) {
    $title = metadata($simple_pages_page, 'title');
    $identifier = metadata($simple_pages_page, 'slug');
    echo $identifier;
}
else 
{
   echo "<p></p>";
}

In the header, the item or any other records may not be available. So the previous approach may not work in it. So another simpler way is to pass the record directly from each page in the function head(), for example in items/show.php:

echo head(array('record' => $item, 'title' => metadata('item', array('Dublin Core', 'Title')), 'bodyclass' => 'items show')); 

Then in your header, you can simplify the code above, since there will be a variable “record” in all pages you updated.

Thank you for the clues @Daniel_KM!! I have passed through an $item in the head array by using the following in the item/show.php which allowed me to extract all the associated item attributes/fields including the identifier without generating an error :slight_smile: :slight_smile:

**head(array(‘record’ => $item,
‘title’ => metadata(‘item’, array(‘Dublin Core’, ‘Title’)),
‘bodyclass’ => ‘item show’)); **

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.