Peter Atkins's blog

Unspecified Errors

For errors that are unspecified this can be very helpful in tracing them.

watchdog('rcpar_partners', 'See ' . __FUNCTION__ . '() ' . $exc->getTraceAsString(), NULL, WATCHDOG_ERROR);

Get Users Profile Url

There may come a time when you need to put links back to a users profile inside your custom module or in a display block. This is one way you can ensure the link is correct and won't produce a 404 message. I've seen some people try and build it using the username value but that has it's own pitfalls. It will not take into account if the user has update their username.

Drupal Debug Backtrace

This is a quick way to debug a code problem.

Option One

$trace = debug_backtrace();
header('content-type: text/plain');
print_r($trace);
exit;

Option Two

User's Roles - Addition and Removal

Programmatically adding or removing roles for existing User accounts.

$role_name = 'your_role'; // The name of the role

// Add a role
if ($role = user_role_load_by_name($role_name)) {
  user_multiple_role_edit(array($user->uid), 'add_role', $role->rid);
}

// Remove a role
if ($role = user_role_load_by_name($role_name)) {
  user_multiple_role_edit(array($user->uid), 'remove_role', $role->rid);
}