I created a function to determine if a page was associated in some way with my blogs. I created a vocabulary is only used by blog types then I create a sql query that checks to see if the current node is associated with this vocabulary, but I've obviously done something wrong b/c I don't get the expected result.

I tested the SQL and got what I was looking for, but when I go to the term page associated with my blogs the function doesn't return true. Any ideas?

function is_blog($node, $title = NULL) {
  // this sql finds if the node is associated with the vocabulary = 'types' which is associated with content type 'blog'
  $sql = sprintf("select count(*) count from term_data d left join term_node n on d.tid = n.tid where d.vid = 1 and n.nid = %d", $node->nid);
  $result = db_query($sql);
  $obj = db_fetch_object($result);
  return $node->type == 'blog' || stripos($title, "'s blog") !== FALSE || strtolower($title) == "blogs" || $title == "Reply to comment" || $obj->count == 1;
}

Comments

nevets’s picture

It depends on how you call is_blog(), how is $node set for the term page?

Also

  $sql = sprintf("select count(*) count from term_data d left join term_node n on d.tid = n.tid where d.vid = 1 and n.nid = %d", $node->nid);
  $result = db_query($sql);

following Drupal practices should be

  $sql = "select count(*) count from term_data d left join term_node n on d.tid = n.tid where d.vid = 1 and n.nid = %d";
  $result = db_query($sql, $node->nid);

I also think count(*) count should be count(tid) as count, I think the 'as' is required and its more efficient to count on a single field.

jpstokes’s picture

the is_blog function is called from within the page.tpl.php file like this:

<?php is_blog($node, $title) ?>

P.S. - thanks for the insight on drupal practices, however unless the 'as' part is a drupal practice it's not necessary.

nevets’s picture

The code

<?php is_blog($node, $title) ?>

has no impact in page.tpl.php, the function is called but nothing is done with the return. I would if you mean to do something like

<?php if ( is_blog($node, $title) ) : ?>
some html
<?php endif; ?>
jpstokes’s picture

Actually I have it in an if statement, I was just summarizing...I guess that's not good when talking about code. Anyway, I do have the if statement and this code works as expected for all pages except one and that page is a taxonomy page. I'm thinking that maybe on that page I shouldn't look at the node but something else.

Just to explain a little better what I'm doing I basically have this in place currently:

<div id="container" <?php if(is_blog($node, $title)) print 'class="blog"'; ?>>
html
</div> <!-- end container -->

Since I couldn't find a way to successfully get all the pages associated with my blogs to show up with in the page-blogs.tpl.php file like the taxonomy pages for blog topics and such, I decided to just detect when the page/node had something to do with a blog by either checking the node type or the page title. If either matched the "patterns" for lack of a better word, then the class "blog" would be added to the container and thus it would take on the css styling for my blogs. Again this works on all of the pages associated with my blog; all but one, the taxonomy page.

nevets’s picture

Taxonomy pages have an unaliases path of the form taxonomy/term/{tid} and their title will not generally have blog in it. So if you only have one vocabulary you could use code like

if ( arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) ) {
  // On a taxonomy page
  return TRUE;
}

If you have more than one vocabulary but only one is of interest you could use something like

$wanted_vid = 1;  // Vocabulary id of the vocabulary you care about
if ( arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) ) {
  // On a taxonomy page
  $term = taxonomy_get_term(arg(2));
  if ( $term->vid == $wanted_vid ) {
    return TRUE;
  }
}
jpstokes’s picture

Thanks for the example. I didn't know about the arg function. I was able to get a modified version of your code to work.


function is_blog_vocabulary (){
$wanted_vid = 1; // Vocabulary id of the vocabulary you care about
if ( arg(1) && is_numeric(arg(2)) ) {
// On a taxonomy page
$term = taxonomy_get_term(arg(2));
if ( $term->vid == $wanted_vid ) {
return TRUE;
}
}
}

Good look.

nevets’s picture

This arg(1) && is_numeric(arg(2)) is very incomplete, it will trigger on any path where arg(2) is a number producing wasted calls to taxonomy_get_term(). For arg(2) to be numeric arg(1) must always be true.

jpstokes’s picture

I agree...I change it to check if arg(1) == 'term'. This works fine. On the blog page I didn't get anything back when executing arg(0) so I took that out since it would cause the blog index page to fail.