Recipes/Navigate Items by Type

Problem

I want to organize my site by item type, so I need users to easily be able to navigate between different item types without having to go through the advanced search page.

Solution

Functions Used

We need to create a function that will get the right URLs when given the names of the item types we want to browse by. This way, we don't need to manually figure out the correct link for each item type.

This function should go in the theme's custom.php file. Some themes already have a custom.php file, and some do not. If the theme you are using does not have a custom.php file, create it yourself in the root of the theme directory.

/**
 * Get an array of item_browse URLs, suitable for use with nav().
 *
 * @param array $itemTypes Array of item type names.
 * @param array $browseParams (optional) Additional browse parameters,
 *  will be used in every URL.
 * @return array
 */
function mytheme_items_browse_by_types($itemTypes, $browseParams = array())
{
    $urls = array();
    $table = get_db()->getTable('ItemType');
    foreach ($itemTypes as $typeName) {
        $itemType = $table->findByName($typeName);
        $urls[$typeName] = uri('items/browse', array('type' => $itemType->id) + $browseParams);
        release_object($itemType);
    }
 
    return $urls;
}

Now, you can use this function wherever you want users to be able to navigate by item type. An easy way to do this is to use the nav() helper, which will output the HTML for a list of links.

$types = array('Document', 'Still Image');
echo nav(mytheme_items_browse_by_types($types));

The second parameter allows you to add browse parameters to every URL. You could use that parameter to create the navigation shown in the "Browse Within a Collection" recipe without having to look up the correct ID for each type yourself.

$types = array('Document', 'Still Image');
echo nav(mytheme_items_browse_by_types($types, array('collection' => collection('id')));