i would like to do the following: add a few lines to taxonomy_select_nodes in taxonomy.module so that i can call a url such as taxonomy/term/8/all/blog and get all blog posts of term 8.
what is have done so far is add some stuff to function taxonomy_select_nodes so that it now looks like this:
function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE) {
if (count($tids) > 0) {
// For each term ID, generate an array of descendant term IDs to the right depth.
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$term = taxonomy_get_term($tid);
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
$descendant_tids[] = array_merge($tid, array_map('_taxonomy_get_tid_from_term', $tree));
}
if ($operator == 'or') {
$str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
$sql = 'SELECT DISTINCT(n.nid), n.type FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql() .' ';
if (arg(4)){
$sql .= 'AND n.type = ' . arg(4) . ' ';
}
$sql .= 'ORDER BY n.sticky DESC, n.created DESC';
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
if (arg(4)){
$sql_count .= ' AND n.type = ' . arg(4) . ' ';
}
}
else {
$joins = '';
$wheres = '';
foreach ($descendant_tids as $index => $tids) {
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
$wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
}
if (arg(4)){
$wheres .= ' AND n.type = ' . arg(4);
}
$sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres .' ORDER BY n.sticky DESC, n.created DESC';
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres;
}
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10) , 0, $sql_count);
}
else {
$result = db_query_range($sql, 0, 15);
}
}
return $result;
}
when i try out a url like: http://mydrupalsite.org/taxonomy/term/19/all/blog i get a page not found error.
i am using the devel module to look at the sql queries to see what is going wrong, but i can't tell (i think) because i am redirected to the page not found error page.
is there somewhere i have to register a page callback or something so that it gives me a valid page? is this method fundamentally flawed? can anyone help?
thanks.
erik
Comments
here is a patch file to make my changes more apparent
[?PHP
Index: taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy.module,v
retrieving revision 1.139
diff -u -r1.139 taxonomy.module
--- taxonomy.module 21 Aug 2004 06:42:37 -0000 1.139
+++ taxonomy.module 27 Aug 2004 17:14:18 -0000
@@ -775,8 +776,15 @@
if ($operator == 'or') {
$str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
- $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql() .' ORDER BY n.sticky DESC, n.created DESC';
+ $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
+ if (arg(4)){
+ $sql .= ' AND n.type = ' . arg(4);
+ }
+ $sql .= ' ORDER BY n.sticky DESC, n.created DESC';
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
+ if (arg(4)){
+ $sql_count .= ' AND n.type = ' . arg(4);
+ }
}
else {
$joins = '';
@@ -785,6 +793,9 @@
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
$wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
}
+ if (arg(4)){
+ $wheres .= ' AND n.type = ' . arg(4);
+ }
$sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres .' ORDER BY n.sticky DESC, n.created DESC';
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres;
}
?]
here is a bit of a flow chart for what i have done
added $type = 'all' to taxonomy_term_page:
added $type to switch ($op) case 'page':
added $type to taxonomy_select_nodes:
Replaced some of the code in taxonomy_select_nodes to:
now when i go to: /taxonomy/term/19/all/page/blog
i get error message:
Fatal error: Unknown column '$type' in 'where clause' query: SELECT COUNT(DISTINCT(n.nid)) FROM node n INNER JOIN term_node tn0 ON n.nid = tn0.nid WHERE n.status = 1 AND '1' AND tn0.tid IN (19,64,41,44,45) AND n.type = $type in /home/commons/public_html/unstable/includes/database.mysql.inc on line 125
and when i go to /taxonomy/term/19/all/blog
i get a Page not found.
ideas?
figured it out... and patch
with lots of help from JonBob and UnConeD, it now works. i have provided a patch here: http://drupal.org/node/view/10443
thanks to both of you again.
erik