Integrate Views 2 with the Blog module

I'm using more and more views on DrupalMU and I needed a way for the pages and blocks to be blog-centric. When you're on a specific blog you only want to see the recent comments on that specific blog, or only see the archive of blog entries by that owner. You also want the blocks to appear on any page of which the blogger is the owner: the user's profile, his blog entry listing, a specific blog entry, the comment page of that entry, ...

In order to determine this kind of context I created a small function that I call as the default argument for the views I use, for instance for the default "recent comments" or the "archive" view. Both can be seen in action on the site when you go to a specific blog.

<?php
function mymodule_context() {
 
$uid = FALSE;
  if((
arg(0)=="user" || arg(0)=="blog") && is_numeric(arg(1))) {
   
// A blogger's profile
   
$uid = arg(1);
  }
  elseif(
arg(0)=="node" && is_numeric(arg(1))) {
   
// A blog entry
   
$node = node_load(arg(1));
    if(
$node->type=="blog") $uid = $node->uid;
  }
  elseif(
arg(0)=="comment" && arg(1)=="reply" && is_numeric(arg(2))) {
   
// A comment page
   
$node = node_load(arg(2));
    if(
$node->type=="blog") $uid = $node->uid;
  }
  elseif(
arg(0)=="archive" && is_numeric(arg(1)) {
   
// The archive view
   
$uid = arg(1);
  }
  return
$uid;
}
?>

It's easy to see what this function does: it provides a context to see who the "owner" of the current page is, and provides this user id back to views. There's no need to configure a block's visibility settings if you're using this function as an argument: if you're not in a specific context then the FALSE is returned and the block is not displayed since Drupal doesn't display empty blocks.

Follow these steps to add this argument default:

  1. Add a new "User: Uid" argument
  2. Select "Provide default argument"
  3. Select "PHP Code"
  4. Fill in "PHP argument code". If you put the code in a custom module you can call that function: return mymodule_context();

You can also put the php code straight in the argument textarea but I would advise against this, especially if you're using this method on multiple views.

Comments

Thanks

Thanks for this, just what I needed for Blog to it.

drupalmu_helper module

Hi Tim,

You might also want to check out the drupalmu_helper module which contains some useful custom modules I'm using for the DrupalMU installation profile. Even if you don't use the module in itself, since it's very specific, I'm sure there are some functions that you could use for your project.

Yep, I have already installed

Yep, I have already installed the module, but havent had much chance to take it apart yet.

Why don't you put this function in that module?
I have also added a feature request to the module to integrate into the profile module, allowing things like, the user changing the name / title of their blog.

The function is in there

The function already is in the module: drupalmu_helper_context(). I'm calling it in most of the views I'm using on DrupalMU.

You could export those views

You could export those views and make them available with the module.

Sign-up for Blog to it let me know what you think.

A couple of questions

Hi: I've installed drupalmu and have followed these instructions to the letter. I hope you can help me with an issue, though:

First: I've started out trying to tweak the 'Archive List' block, getting it to display entries specific to the user's blog.

While the helper function is clearly seeing (and returning) the user id, the block continues to display results for the whole site. Now, however, when I click on the link, the archive page displays a 'Page Not Found' message.

This is probably something simple, and I am definitely emptying the cache after trying this update.

Any ideas? Thanks!

Custimize the archive view

You actually need to customize the archive view. I added the user id as an argument before the "Node: Created year + month" argument. For this argument you can put the php code return drupalmu_helper_context(); as default argument. This will most likely require some trial and error so go ahead and play with it.

That did it!

Thanks for the help: Moving the drupalmu_helper_context() function argument Before the Node:created year+month did the trick! This is really great. Thank you!

Post new comment

The content of this field is kept private and will not be shown publicly.