Monthly Archive for April, 2008

Drupal: View all nodes created by a user

I found myself needing to automatically get back all the albums created by a specific user of a site I am working on. Originally I thought that this was going to be a bit more of a challenge. The hardest part ended up being that I found several ways of doing it and each of them were different. I ended up getting the result that I wanted but, not as fast as I wanted it. So maybe this will help someone else trying to accomplish a similar task.

For the process I used you will need the CCK module and the Views module. Then simply follow the steps below to view a list of nodes created by the user that is logged in.

  1. Create your content type using the CCK
  2. Go to the views admin and create a new view
  3. Check the ‘provide page’ check box
  4. Specify a url path for the view (in my case I used album/user)
  5. In the fields section set the fields that you want to show up (this is only useful in list view really)
  6. Set the arguments (this is the most important step, it lets you get nodes by a user)
    ->User: UID is Author - Default: Return page not found
  7. Expand the Argument Handling Code and paste this in it
    if (!args[0]) {
      global $user;
      $args[0] = $user->uid;
    }
     
  8. Set the filters
    ->Node:Type - is one of - (the node type you created)
    ->Node: Published = Yes
  9. Save the view

Now when you navigate to the URL that you supplied in your URL field for the view you should see any nodes of the type you chose. The custom argument handling we added will set the user id to the current logged user if one isn’t specified.

This view was helpful because I was able to use the Node Reference module for the CCK to add a node reference to the albums in my Songs node. Now I had a drop down list of the users albums so that they can relate the new song to an album, but only one of their albums.

Drupal: Pathauto bulk update on specific user roles

Recently I was faced with the problem of needing to run a bulk update in the path auto module for Drupal. Seemed easy enough, but there was one slight problem. Two of my paths had the same alias but different destinations. Users flagged as fans needed one set of destination => alias pairs and artists needed the same alias but with a different destination. The code is fairly simple, but I wanted to share it because it may help someone else. The one query may come in handy for others checking if a user has a specific role they created in their Drupal installation.

<?php
function event_pathauto_bulkupdate() {
  // get the users we need to loop through
  $query = "SELECT uid, name, src, dst FROM {users} LEFT JOIN {url_alias} ON CONCAT(’events’, uid) = src WHERE uid > 0 AND src IS NULL";
  $result = db_query($query);
 
  // set some vars to store stuff in
  $count = 0;
  $placeholders = array();
  $users = array();
 
  // loop through all of the users and get their roles
  // if they have the role of artist then add their user object to our array
  while ($row = mysql_fetch_object($result)) {
    $data = db_query("SELECT r.name as name FROM {role} r, {users_roles} ur, {users} u WHERE r.rid = ur.rid AND u.uid = ur.uid and ur.uid = %d", $row->uid);
    $role = db_result($data);
   
    if ($role == ‘artist’) {
      $users[] = $row;
    }
  }
 
  // loop through our artists and bulkupdate them
  foreach ($users as $user) {
    $placeholders = pathauto_get_placeholders(‘user’, $user);
    $src = ‘event/user/’. $user->uid;
    if ($alias = pathauto_create_alias(‘event’, ‘bulkupdate’, $placeholders, $src, $user->uid)) {
      $count++;
    }
  }
 
  drupal_set_message(format_plural($count,
    "Bulk generation of user events completed, one alias generated.",
    "Bulk generation of user events completed, @count aliases generated."));
}
 

File include based on URL

While working on a project I found myself needing to dynamically include a snippet based on the URL. Doing this would allow me to keep a majority of my PHP code outside of my template, thus allowing me to easily redesign the layout when I needed to. Below was the result:

<?php
  $query_string = substr($_SERVER[‘QUERY_STRING’], 2);
  $match = preg_match(‘!fans/(.+?)/shows!’, $query_string, $value);
 
  $doc_root = $_SERVER[‘DOCUMENT_ROOT’].‘/’;
  $theme_path = ‘path/to/theme/’;
  $filename = ’snippet-’.str_replace(‘/’, ‘-’, $value[0]).‘.tpl.php’;
 
  $file = $doc_root.$base_path.$theme_path.$filename;
 
  if (file_exists($file)) {
    include($filename);
  } else {
    print $content;
  }
 

Cut the cord…

I think I can! Shutdown Day 2008 is coming up soon. May 3, 2008 is Shutdown Day this year, which means go outside. Log out of World of Warcraft, stop tea bagging random people in Halo, and stop camping that doorway in COD 4 and head outside for some real life fun. If you don’t have friends then this will be a great time for you to get out, meet new people and make some.

As for me, this will give me a great chance to rip up the ugly bushes in my backyard and plant some new ones. Maybe even mow my own lawn instead of paying the neighbors kid to do it. My job locks me in to using the computer on a daily basis, but now on this one day I am going to do the unthinkable and venture outside… I better buy some sunscreen, SPF 90.

Help spread the word about Shutdown Day 2008 by adding the badge to your own Wordpress blog. You can get the plugin from blog.projectx4

Back to Wordpress

I decided to switch things up and go back to Wordpress. I just want a blog, no community… I have enough friends. Ok, I don’t have any. But really, Wordpress is just easier for me. Drupal is cool, but it does so much more than i need. So back to the basics. It will take me a bit, but I will be posting some code snippets and how-to items here in the near future.




Bad Behavior has blocked 57 access attempts in the last 7 days.