Drupal 6, Content Profile default username

I have a content profile set up on a number sites and I noticed when I used User / Node Import recently that I couldn’t assign an import value to the title field of the node profile, which I was using as First Name.
After I did the import. I found out why. The “title” field was defaulted with the Drupal user name.
Which is a very good idea.

So I updated my other content profiles to use this same convention but then I realized that there’s no easy way to default that field value to username.
So I considered a

  • Custom module that overrides the content type Profile and defaults the title with the username. – This is the best option
  • jQuery script in a block that runs on the profile edit pages – This is quick but may miss some pages and relies on javascript and possibly my profile set up (show in tabs)
  • jQuery script in a Views block – This may be slightly better than the static script as it pulls the user name from the database, but it’s is more effort

I settled on the simple jQuery script to make it quick.
I pull the user name from the title of the edit page, which in my configuration is the user name.
I don’t know if this is always the case but it was in this case so it made it easy to use jQuery.

I could potentially use this script in a custom module. But staying server-side and preprocessing the fields would make more sense.
I could also set the script to run anytime the edit page is opened.
The jQuery / Block method relies on known, url configurations that == the profile edit page.
For example: Right now the block is set to
Show on only the listed pages.
With these urls:

user/*/edit/profile
user/*/profile/profile*

I couldn’t remember if this would work with out ‘clean URLs’ enabled but a quick test with

$_SERVER['QUERY_STRING']

showed that the query string is still ?q=user/*/edit/profile. So these listed urls should continue to work.
If I ever include the user profile in a page wizard workflow this block probably wouldn’t show up so that’s another point for the custom module method.

Notes on the code:

I used the usual method (well it’s becoming my usual method) of wrapping the js in a php HEREDOC and appending it to the page with drupal_add_js.
Then I put the script inside Drupal.behaviors so that I runs after jQuery is available.
After that I try to limit the firing to ‘page load’ only.
The only if statement checks to see if the field is empty before dropping the title of the form into the the field using jQuery.

Here’s the code I used:

<?php
//attach a script that defaults the username in a blank Profile.
$addJS = <<

Notes on future development

If I end up using this on another site then I’ll probably consider putting it into a custom module but we’ll wait and see.
It would require that I track down the

  • content type name
  • the right form preprocess override function
  • as well as the address of the field in the node form stdClass Object / Array

Then it’s always nice to include help documentation.
An admin settings form would be nice but I can’t think of any settings…
Dependencies would be the content profile module.

Leave a Comment