1

I'm using prepopulate to prepopulate a feild of a node/add/content. I'm trying to generate a URL using some PHP code that willl generate a URL like:

http://mysite.com/node/add/content_type?edit[title]={$some_var}

Each of my user profiles will have this link at the top of the page and I would like $some_var to hold the username of the user whos profile we are currently viewing. I was wonder how to populate $some_var with the username of the user profile being viewed.

Something like:

$some_var = $username;
print("<a href=\"http://mysite.com/node/add/content_type?edit[title]={$some_var}\">Content Type</a>");
1

If this link is to be on the user profile page then you can grab the related user ID from the internal URL (using the arg() function), and then use user_load() to load a user object that contains the username:

// Get user ID from internal router path (this isn't necessarily the URL shown in the address bar, it's the internal path that Drupal is using to serve the page).
$uid = arg(1);

// Load the user account and extract the username
$account = user_load($uid);
$username = $account->name;
  • works perfectly! – Mike May 14 '12 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.