Skip to content

Instantly share code, notes, and snippets.

@chadsten
Created March 29, 2017 19:45
Show Gist options
  • Save chadsten/c474ebbdd57e9bc40d3c9739c4fd0fb8 to your computer and use it in GitHub Desktop.
Save chadsten/c474ebbdd57e9bc40d3c9739c4fd0fb8 to your computer and use it in GitHub Desktop.
<?php
/*
* Gets logged in user's Organic Group ID
*/
function og_taxonomy_menu_get_user_group() {
$groups = og_get_groups_by_user();
if (is_array($groups)) {
$group = array_values($groups['node']);
$group = $group[0];
$group = node_load($group);
} else {
// load default DT group
$group = node_load('2456');
}
return $group->nid;
}
/*
*
*/
function og_taxonomy_menu_get_terms($vocabulary) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary);
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));
return $terms;
}
function og_taxonomy_menu_get_term_nodes($tid) {
// seems this will only query what the current user has access to
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'commerce_product')
->fieldCondition('field_product_category', 'tid', $tid);
$result = $query->execute();
if (is_array($result['node'])) {
$nids = array_keys($result['node']);
}
return $nids;
}
/*
* Makes menu block available in block management.
*/
function og_taxonomy_menu_block_info() {
$blocks['og_taxonomy_menu'] = array(
'info' => t('Browse by Category'),
);
$blocks['og_taxonomy_menu_fire'] = array(
'info' => t('Browse by Category'),
);
return $blocks;
}
/*
*
*/
function og_taxonomy_menu_filter_results($vocabulary) {
$tids = og_taxonomy_menu_get_terms($vocabulary);
foreach ($tids as $tid) {
// get array of NIDs for this TID
$nids = og_taxonomy_menu_get_term_nodes($tid->tid);
if ($nids) {
// add the NIDs to the flat array structure
foreach ($nids as $nid) {
$nid_list[] = $nid;
}
}
}
foreach ($nid_list AS $nid) {
// query all taxonomy reference fields
// @TODO abstract to allow vocabulary arg filter to be passed
// @TODO add Published == true
// @TODO add node type check? MAYBE
$results = db_query('SELECT tid FROM {taxonomy_index} WHERE nid = :nid', array(':nid' => $nid));
foreach ($results as $result) {
$term = taxonomy_term_load($result->tid);
if ($term->vocabulary_machine_name == $vocabulary) {
$terms[] = $term->tid;
}
}
}
$children = array_unique($terms);
$tree = array();
$html = '';
foreach ($children as $child) {
$parent_tid = taxonomy_get_parents($child);
$parent_tid = array_values($parent_tid);
$parent_tid = $parent_tid[0]->tid;
$tree[$parent_tid]['items'][preg_replace("/[^A-Za-z0-9 ]/", '', taxonomy_term_load($child)->name)] = $child;
$tree[$parent_tid]['weight'] = taxonomy_term_load($parent_tid)->weight;
}
// sort parent items by weight
uasort($tree, 'og_taxonomy_menu_sort_by_weight');
$html .= '<ul class="menu">';
foreach($tree as $parent_tid => $children) {
$parent_link = og_taxonomy_menu_build_link($parent_tid);
$html .= '<li class="menu__item">' . $parent_link;
$html .= '<ul class="menu">';
// sort children alphabetically, since weights are non-unique
ksort($children['items']);
foreach($children['items'] as $child_tid) {
$child_link = og_taxonomy_menu_build_link($child_tid);
$html .= '<li class="menu__item is-leaf leaf">' . $child_link . '</li>';
}
$html .= '</ul>';
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
function og_taxonomy_menu_sort_by_weight($a, $b) {
return $a['weight'] - $b['weight'];
}
function og_taxonomy_menu_build_link($tid) {
$tid = taxonomy_term_load($tid);
$uri = taxonomy_term_uri($tid);
$path = drupal_get_path_alias($uri['path']);
$link = "<a href='/" . $path . "'>" . $tid->name . "</a>";
return $link;
}
function og_taxonomy_menu_block_view($delta) {
$block = array();
switch ($delta) {
//$nids = og_taxonomy_menu_filter_results();
case 'og_taxonomy_menu':
$block['subject'] = t('Browse by Category');
$block['content'] = og_taxonomy_menu_filter_results('catalog');
break;
case 'og_taxonomy_menu_fire':
$block['subject'] = t('Browse by Fire Category');
$block['content'] = og_taxonomy_menu_filter_results('fire_catalog');
break;
}
return $block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment