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.
- Create your content type using the CCK
- Go to the views admin and create a new view
- Check the ‘provide page’ check box
- Specify a url path for the view (in my case I used album/user)
- In the fields section set the fields that you want to show up (this is only useful in list view really)
- 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
- Expand the Argument Handling Code and paste this in it
if (!args
[0]) {
global $user;
$args[0] =
$user->
uid;
}
- Set the filters
->Node:Type - is one of - (the node type you created)
->Node: Published = Yes
- 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.
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."));
}