Add breadcrumbs to farm taxonomy term pages, and allow other modules to add breadcrumbs to them via a new hook_farm_taxonomy_breadcrumb().

This commit is contained in:
Michael Stenta 2015-04-16 12:10:27 -04:00
parent 37e41d8a15
commit bfa421c0a4
2 changed files with 63 additions and 2 deletions

View File

@ -22,10 +22,30 @@
* farm_taxonomy.
*/
/**
* Add breadcrumbs to the taxonomy view page.
*
* @param object $term
* The taxonomy term entity.
*
* @return array
* Returns an array of links to add to the term's breadcrumb.
*/
function hook_farm_taxonomy_breadcrumb($term) {
$breadcrumb = array();
// If the asset is an animal, add a link to /farm/animals.
if ($farm_asset->type == 'animal') {
$breadcrumb[] = l(t('Animals'), 'farm/animals');
}
return $breadcrumb;
}
/**
* Attach Views to taxonomy term pages.
*
* @param array $term
* @param object $term
* The taxonomy term entity.
*
* @return array
@ -38,7 +58,7 @@
* 'weight' - the weight of the View in the taxonomy page
* (this is useful for changing the order of Views)
*/
function hook_taxonomy_term_view_views(array $term) {
function hook_taxonomy_term_view_views($term) {
// If the term is not a crop, bail.
if ($term->vocabulary_machine_name != 'crop') {

View File

@ -24,6 +24,9 @@ function farm_taxonomy_entity_view_alter(&$build, $type) {
// Pull the term entity out of the build array.
$term = $build['#term'];
// Set the term page breadcrumb.
_farm_taxonomy_term_set_breadcrumb($term);
// Invoke hook_farm_taxonomy_term_view_views() to get a list of Views.
$views = module_invoke_all('farm_taxonomy_term_view_views', $term);
@ -100,3 +103,41 @@ function farm_taxonomy_entity_view_alter(&$build, $type) {
);
}
}
/**
* Helper function for setting farm taxonomy term page breadcrumbs.
*
* @param object $term
* The taxonomy term.
*/
function _farm_taxonomy_term_set_breadcrumb($term) {
// Ask other modules for breadcrumbs for this term.
// We don't know if this term is part of a farm vocabulary yet, so we will
// only set the breadcrumb if this returns results.
$module_breadcrumbs = module_invoke_all('farm_taxonomy_breadcrumb', $term);
// If other modules provided breadcrumbs...
if (!empty($module_breadcrumbs)) {
// Get the default breadcrumb.
$default_breadcrumb = drupal_get_breadcrumb();
// Shift the first item off (home) and start a new breadcrumb array with it.
$home = array_shift($default_breadcrumb);
$breadcrumb = array($home);
// Add "Farm".
$farm = l(t('Farm'), 'farm');
$breadcrumb[] = $farm;
// Add the breadcrumbs provided by other modules.
$breadcrumb = array_merge($breadcrumb, $module_breadcrumbs);
// Finally, put the original breadcrumbs back onto the end.
$breadcrumb = array_merge($breadcrumb, $default_breadcrumb);
// Set the breadcrumb.
drupal_set_breadcrumb($breadcrumb);
}
}