function taxonomy_menu_block_build in Taxonomy menu block 7
Function to build our tree.
Parameters
array $config: An array of configuration options that specifies how to build the taxonomy tree
- admin_title: Administrative title to use on the block admin page
- vid: Taxonomy vocabulary id
- parent: What kind of tree we will build 0: Display the whole tree 1: Fixed parent: display only a branch of tree, parent is given 2: Dynamic tree: will look at currently viewed page, find an active tid and display the children of that tid
- parent_fixed: The fixed parent tid in case parent is set to 1
- parent_dynamic: Determines at what depth we need to find the parent tid in case parent is set to 2
- depth: Until what depth we need to render our tree.
- home_link: Whether to add a link to the frontpage at the beginning of the menu or not.
Return value
array Array that contains one value: content, which is the fully rendered HTML of our tree
1 call to taxonomy_menu_block_build()
- taxonomy_menu_block_block_view in ./
taxonomy_menu_block.module - Implements hook_block_view().
File
- ./
taxonomy_menu_block.module, line 145 - Taxonomy Menu Block module allows you to make menu blocks out of your taxonomies in a very performant way.
Code
function taxonomy_menu_block_build($config) {
// Initialize.
$tree = array();
$parent = NULL;
$data['content'] = '';
$data['subject'] = '';
// Get tree.
$taxonomy = taxonomy_menu_block_get_taxonomy($config['vid']);
$tree = $taxonomy['tree'];
// Find active tid.
$tid = taxonomy_menu_block_find_active_tid($tree, $config['vid']);
// Let other modules alter the active tid.
drupal_alter('taxonomy_menu_block_active_tid', $tid, $config);
// If we have a dynamic parent and no active tid, bail out.
if ($config['parent'] == '2' && !$tid) {
return;
}
// Add the active trail.
$tree = taxonomy_menu_block_add_active_trail($tree, $tid);
// Set parent for fixed parent blocks.
if ($config['parent'] == '1') {
$parent = $config['parent_fixed'];
}
// If we have a dynamic parent, find the parent tid.
if ($config['parent'] == '2') {
if ($config['parent_dynamic'] != '0') {
// Loop over tree and find the highest item in active trail, taking into
// account our specified depth.
foreach ($tree as $key => $term) {
if ($term['active_trail'] != '0' && $term['depth'] == $config['parent_dynamic'] - 1) {
$parent = $key;
}
}
}
else {
$parent = $tid;
// Store depth for later use.
$parent_depth = $tree[$parent]['depth'];
}
// Since we know we have an active tid, we should have a parent.
if (!$parent) {
return;
}
}
// Set title of block. Do it now when we still have the full tree, in case of
// a valid parent.
if ($parent) {
$data['subject'] = check_plain($tree[$parent]['name']);
}
else {
$data['subject'] = check_plain($taxonomy['name']);
}
// Trim tree to one branch.
if ($parent) {
// Loop over tree, store all items that have higher depth than parent.
// First item that has the same depth means new branch of tree.
$start = FALSE;
$branch = array();
foreach ($tree as $key => $term) {
// Stop storing once we hit a term with same depth as $parent.
if ($term['depth'] <= $tree[$parent]['depth']) {
$start = FALSE;
}
if ($start == TRUE) {
$branch[$key] = $term;
}
// Only start storing from when we hit $parent.
if ($key == $parent && $start != TRUE) {
$start = TRUE;
}
}
// An empty array means that we don't have any children for this branch.
if ($branch) {
$tree = $branch;
}
else {
return;
}
}
// If we have to add the number of nodes attached to each term.
if ($config['nodes']) {
$tree = taxonomy_menu_block_nodes($tree, $config);
}
// If we have to remove the terms with no nodes attached.
if ($config['hide_empty']) {
$tree = taxonomy_menu_block_hide_empty($tree, $config);
}
// Depth trim. Always do this last as to not lose the active trail.
if ($config['depth'] != '0') {
if ($config['parent'] == 2 && $config['parent_dynamic'] == 0) {
// Depth is relative to the parent term's depth.
$max_depth = $parent_depth + $config['depth'];
}
else {
// Depth is relative to the root. Substract one of our max depth
// because Drupal considers 0 to be the root level.
$max_depth = $config['depth'] - 1;
}
$temp_tree = array();
foreach ($tree as $tid => $term) {
if ($term['depth'] <= $max_depth) {
$temp_tree[$tid] = $term;
}
}
if ($temp_tree) {
$tree = $temp_tree;
}
else {
return;
}
}
// Last check. If we for some reason still don't have a built tree, bail.
if (empty($tree)) {
return;
}
// Add link to frontpage.
if ($config['home_link']) {
$home_link = array(
// Give it an impossible key so it doesn't get mistaken for a parent tid.
'-1' => array(
'name' => t('Home'),
'path' => '<front>',
'depth' => '0',
'parents' => array(
'0' => $parent ? $parent : '0',
),
'active_trail' => drupal_is_front_page() ? '2' : '0',
),
);
// Add to beginning of $tree array.
$tree = $home_link + $tree;
}
// Let other modules alter the taxonomy tree data after all the processing has
// been done.
drupal_alter('taxonomy_menu_block', $tree, $config);
// Nest tree.
// If we have a parent, pass along parent.
if ($parent) {
$tree = taxonomy_menu_block_nest_tree($tree, $parent);
}
else {
$tree = taxonomy_menu_block_nest_tree($tree);
}
// Theme tree.
$data['content'] = array(
'#items' => $tree,
'#config' => $config,
'#theme' => 'taxonomy_menu_block__' . $config['delta'],
);
return $data;
}