function weblinks_get_tree($parent = 0, $quick = FALSE, $ismainlinkspage = TRUE) { dd('-- weblinks_get_tree --'); dd($parent, '$parent'); $admin = user_access('administer weblinks'); $taxo_img = module_exists('taxonomy_image_x'); // @@@ $collapsible = variable_get('weblinks_collapsible', TRUE); $collapsed = variable_get('weblinks_collapsed', FALSE); $empty_text = variable_get('weblinks_empty_text', NULL); $skip_empty = empty($empty_text); if ($admin) { $skip_empty = FALSE; } $show_desc = variable_get('weblinks_catdesc', TRUE); $format = variable_get('weblinks_format', filter_fallback_format()); $max_depth = $admin ? 99999999 : variable_get('weblinks_maxfrontdepth', 1); $tree = array(); if (module_exists('taxonomy')) { $tree = taxonomy_get_tree(_weblinks_get_vocid(), $parent); } dd($tree, '$tree starting from ' . $parent); // Is this a top level request? if ($parent) { dd('Has parent - not top level'); // The existing elements have depths one too low. foreach ($tree as $term) { // Increase the depth of each term by 1, so that we can insert the parent // at depth 0 afterwards. ++$term->depth; } // Not top level, so we need to get the requested term // and stick it on the front of the tree. dd(module_exists('taxonomy_xml') ? 'TRUE' : 'FALSE', "module_exists('taxonomy_xml')"); $parent_term = taxonomy_term_load($parent); dd($parent_term, '$parent_term returned by taxonomy_term_load(' . $parent . ')'); dd(isset($parent_term->parents) ? '+++ YES +++ unexpected' : 'No, as expected', 'isset($parent_term->parents)'); array_unshift($tree, $parent_term); // Put parent term at top of array and set depth to 0. $tree[0]->depth = 0; dd($tree, 'First modified $tree'); // The above can be replaced by this: if ( $parent_list = array_keys(taxonomy_get_parents($parent)) ) { dd($parent_list, '$parent_list'); $tree = taxonomy_get_tree(_weblinks_get_vocid(), $parent_list[0]); dd($tree, 'New $tree'); } } else { dd('Top level'); // Top level, so do we have unclassified links? $unclassed_count = db_query(_weblinks_get_query(0, 'count'))->fetchField(); if ($admin || $unclassed_count > 0) { // Add our dummy unclassified term object to the list. $tree[] = _weblinks_unclassed(); } // Do we want a separate unpublished group? if (variable_get('weblinks_unpublished_title', t('Unpublished'))) { // Do we have unpublished nodes? $unpub_count = db_query(_weblinks_get_query('unpublished', 'count'))->fetchField(); if ($admin && $unpub_count > 0) { $tree[] = _weblinks_unpublished(); } } dd($tree, 'Top level $tree'); } $new_tree = array(); dd('>>> processing $tree again, to create $new_tree'); foreach ($tree as $term) { dd($term, '$term'); $tid = $term->tid; // If we are too deep already, or the term should be hidden from the main // links page, skip the whole term. $show_this_term = variable_get('weblinks_page_' . $tid, TRUE); if (!$admin && ($term->depth > $max_depth || ($ismainlinkspage && !$show_this_term))) { continue; } // If we are suppressing empty terms and there are no links in this group, skip it. if ($tid === 0) { $term->node_count = $unclassed_count; } else { if ($tid == 'unpublished') { $term->node_count = $unpub_count; } else { // $term->node_count = db_query("SELECT COUNT(ti.nid) FROM {taxonomy_index} as ti INNER JOIN {node} as n on n.nid=ti.nid WHERE ti.tid = :aid and n.type='weblinks'", array(':aid' => $tid))->fetchField(); dd($tid, 'callings weblinks_term_node_count for tid'); $term->node_count = weblinks_term_node_count($tid, 'weblinks', TRUE); } } if ($skip_empty && $term->node_count == 0) { continue; } $new_tree[$tid] = $term; $new_tree[$tid]->children = array(); dd(isset($term->parents) ? 'TRUE' : 'FALSE', 'isset($term->parents)'); if (isset($term->parents)) { $keys = array_keys($term->parents); dd(($keys[0] != 0) ? '+++ NOT ZERO +++' : '0 as expected', '$term->parent first key'); $x = $term->parents; dd(array_pop($x), 'first item'); // what is this trying to do? // If we have $term->parents and $term->parents[0] is not 0 then ... // for each $term->parents if the $parent term is in $new_tree then ... // add the current term $tid into the children array for $new_tree[$parent] dd(isset($term->parents[0]) ? 'TRUE' : '>>> FALSE <<<', 'isset($term->parents[0])'); // Set the 'children' array ........................n this term to set the 'children' of tje te the if ($term->parents[0] != 0) { // can cause undefined offset '0' see https://www.drupal.org/node/2197719 dd('$term->parents[0] is not 0. +++ do something here +++'); foreach ($term->parents as $parent) { dd(isset($new_tree[$parent]) ? 'TRUE' : 'FALSE', 'isset($new_tree[$parent])'); if (isset($new_tree[$parent])) { dd("add $tid to ->children array"); $new_tree[$parent]->children[] = $tid; } dd(isset($new_tree[$parent]->children) ? $new_tree[$parent]->children : 'not set', '$new_tree[' . $parent . ']->children'); } } } $new_tree[$tid]->limit = variable_get('weblinks_maxdisp_'. $tid, ''); $new_tree[$tid]->sort = variable_get('weblinks_group_sort_'. $term->tid, ''); // Collapsible is more complicated than just the setting. $new_tree[$tid]->collapsible = $collapsible && ($new_tree[$tid]->depth < $max_depth) && !empty($term->name); $collapse_me = variable_get('weblinks_collapse_'. $tid, FALSE); $new_tree[$tid]->collapsed = $new_tree[$tid]->collapsible && ($collapsed || $collapse_me || ($new_tree[$tid]->depth > $max_depth)); if ($new_tree[$tid]->collapsible) { $new_tree[$tid]->title = check_plain($term->name); } else { if (variable_get('weblinks_linktitle', TRUE)) { $new_tree[$tid]->title = l($term->name, 'weblinks/'. $tid); } else { $new_tree[$tid]->title = check_plain($term->name); } } $new_tree[$tid]->desc = NULL; if ($show_desc && $new_tree[$tid]->collapsible) { if ($term->description) { $new_tree[$tid]->desc = ''; } } else { $new_tree[$tid]->desc = ''; } // Position first node after whatever description there was. $new_tree[$tid]->desc .= '
'; $new_tree[$tid]->image = $taxo_img ? '' : NULL; } dd($new_tree, 'returning $new_tree'); dd('-- weblinks_get_tree -- END'); return $new_tree; }