Daily Archive for April 18th, 2008

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."));
}
 



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