HOWTO: Liberating the User Picture Upload Interface
One of the things that makes Drupal such a powerful platform for building community websites is its robust and scalable user system. Its got everything you need to start popin’ and lockin’ built right into the core framework: unlimited user roles, granular permissions, easily extensible profiles and more.
Still, as hookable and mashup-friendly as it is, there are a number of things that Drupal core has a stubborn hold on. One of these is the user-picture (aka avatar) upload interface. The picture system is very well implemented — it will resize uploaded images, display them in posts and comments automagically, etc — but the user-interface for you to actually put up your picture is locked onto the catch-all “account settings” part of the user/edit interface, which can get pretty cluttered if you’ve got a lot of modules throwing their settings in there.
In future (5.0+) revisions, I’m sure that the already-awesome profile system will grow and allow site admins to easily reposition all user-account elements, but yesterday I had a need to move that user-picture upload field into it’s own page, and so I did. Less than 50 lines of code, too. Here’s how:
Step 1: Create a new section in the user/edit interface
For my application, I wanted to put the user-picture upload off on it’s own section of the user/edit interface, on it’s own “category” in Drupal’s user/edit lingo. This would separate it from the “account settings” clutter, and also give me a place to expand if I want to add other user-picture features down the line.
The first order of business is making a new category. For this, I used hook_user and returned some data when the $op was looking for “categories”:
<?php
function avatar_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'categories') {
$categories = array(array('name' => 'avatar', 'title' => t('picture'), 'weight' => 10));
return $categories;
}
}
?>The data structure you return is a little complex there. You return an array of associative arrays, with each associative array defining a new category. You set an internal name, a human readable title, a weight to help set the order when there are multiple categories. Since I only want one new category, I have an array of one arrays, as you can see.
This sets me up to now tell drupal to display a form when the user clicks on this category.
Step 2: Define your form
Within the same instance of hook_user, I added the following code:
<?php
if ($op == 'form' && $category == 'avatar') {
$form = array();
$form['avatar'] = array(
'#type' => 'fieldset',
);
$form['avatar']['preview'] = array(
'#type' => 'markup',
'#value' => theme('user_picture', $account),
'#prefix' => '<h2>'.t('Your current picture').'</h2>',
);
$form['avatar']['picture_upload'] = array(
'#type' => 'file',
'#title' => t('Upload a new picture'),
'#size' => 20,
);
return $form;
}
?>When the $op is “form,” Drupal listens for a Form API-style array to render on that category page of the user/edit interface. You can also use this to extend the form on other category pages, including the default “account settings” page.
What I’ve defined here tells the “avatar” category to display the current user-picture via a “markup” form element, and then defines a file field for uploading a new one. I’ve borrowed heavily from the core user.module and kept it simple, but this could someday expand to include other picture-related options, such as multiple images, a caption, etc.
With this in place, can now move on to handling the submitted form values.
Step 3: Handling form values
Again, we’re utilizing hook_user, this time to deal with a submitted form by means of the “validate” $op value:
<?php
if ($op == 'validate' && $category == 'avatar') {
if ($file = file_check_upload('picture_upload')) {
user_validate_picture($file, $edit, $account);
}
}
?>This seems too easy to be true, but it isn’t. Since I’ve stuck with the established style for Drupal’s default user-picture upload, and I don’t want to change how the stored pictures are handled (just where the upload interface lives), I can reuse the function in the core user.module to handle the form values.
If I wanted to handle multiple images or additional data, this would have to be re-written and expanded, but for now we can take advantage of the functionality Drupal already offers to finish up our user/edit category.
Step 4: Hiding the old form elements
One last detail is to get rid of the old user-picture upload interface. It will still work fine, but it would be confusing to users to have this interface in two places. Luckily, there’s no need to hack at Drupal core’s user.module thanks again to Form API and the form_alter hook:
<?php
function avatar_form_alter($form_id, &$form) {
if($form_id == 'user_edit' && arg(3) == NULL) {
// only fire this if it's user/<uid>/edit, not any other category
unset($form['picture']);
}
}
?>This clears out the old user-picture upload interface, preventing user confusion.
Guess what? You’re done! You can download the complete module code below. Share and enjoy!
| Attachment | Size |
|---|---|
| avatar.module.gz | 671 bytes |













http://www.mmoinn.com
You buy wow gold may compare prices, but mmoinn.com players know what they need, we provide you with the cheapest wow gold , you can find at any time wow power leveling . Mmoinn in trade, you can enjoy world-class service.
Thanks for your Overview
Thanks for your helpful information. I really like your help very much. Keep it coming. Thanks for your informative help. Done a great job.
wow gold
wow gold
wow leveling
hero online gold
last chaos gold
ragnarok zeny
ro zeny
rappelz rupee
rappelz gold
Silkroad gold
buy silkroad gold
SWG Credits
star wars galaxies credits
tales of pirates gold
age of conan
age of conan gold
aoc gold
aoc power leveling
anarchy online credits
anarchy online credit
archlord gold
buy archlord gold
City of Heroes Infamy
coh influence
City of Villains infamy
cov infamy
Gaia Online Gold
gaia gold
ge money
granado espada vis
granado espada gold
hellgate london palladium
Hero online gold
buy hero gold
Last Chaos Gold
buy Last Chaos Gold
gw gold
Guild Wars Gold
Lineage 2 adena
buy lineage 2 adena
Tales of Pirates Gold
Tales Of Pirates Online
rappelz rupee
rappelz gold
Hellgate London
hellgate london palladium
Second Life Linden
second life money
Vanguard money
Buy Vanguard Gold
Tabula Rasa Credit
tabula rasa credits
wow gold
ffxi gil
Maple Story Mesos
wow gold
wow gold
buy Warhammer gold
Warhammer gold
buy Warhammer gold
Warhammer gold
wow gold
wow gold
age of conan gold
aoc gold
age conan gold
age of conan power leveling
aoc power leveling
age conan gold
age of conan gold
aoc gold
aoc power leveling
Runescape Money
Runescape Gold
Runescape Items
Runescape Money
Runescape Gold
Runescape Power leveling
Runescape Money
Runescape Gold
Runescape gold
runescape money
Runescape Money
Runescape
Runescape Money
Runescape Power leveling
Runescape Items
runescape money
money runescape
Runescape Gold
age of conan gold
age conan gold
aoc gold
age of conan power leveling
aoc power leveling
aoc leveling
archlord gold
anarchy online credits
cabal alz
city of heroes influence
coh influence
city of heroes
city of villains infamy
cov infamy
city of villains
dofus kamas
kamas dofus
dofus
daoc gold
daoc plat
eve isk
eve online isk
eve online
everquest 2 gold
eq2 plat
Ever Quest 2
final fantasy xi gil
Final Fantasy XI
buy ffxi gil
final fantasy 11 cheap gil
gaia online gold
gaia gold
guild wars gold
gw gold
guild wars
hellgate london palladium
Hellgate London
dofus kamas
dofus gold
dofus
dofus kamas
dofus
wow gold
world of warcraft gold
buy wow gold
cheap wow gold
wow power leveling
eq plat
EverQuest gold
Maple Story Mesos
MapleStory mesos
Maple Story meso
Maple Story Meso
maple Story Mesos
MapleStory mesos
Final Fantasy XI gil
ffxi gil
buy ffxi gil
dofus kamas
kamas dofus
lotro gold
lotr gold
lord of the ring gold
Lineage adena
Lineage 1 adena
Silkroad gold
buy silkroad gold
flyff penya
buy flyff gold
flyff money
eve online isk
eve isk
2moons dil
2moons gold
eq2 plat
EverQuest 2 gold
EverQuest 2 plat
Runescape gold
Runescape money
maplestory Mesos
MapleStory meso
maple story
Maple Story Mesos
maplestory Mesos
MapleStory meso
Maple Story
Lineage adena
Lineage 1 adena
Lineage 1
lotro gold
lotr gold
The Lord Of The Ring
lord of the ring gold
ge money
granado espada vis
granado espada gold
Granado Espada
data processing service
data processing
data process
data processing service
data processing
data process
data processing service
data processing
data process
dofus kamas
kamas dofus
buy kamas
dofus kamas
kamas dofus
buy kamas
dofus kamas
kamas dofus
buy kamas
dofus kamas
kamas dofus
buy kamas
dofus kamas
kamas dofus
buy kamas
Are you
Are you wholesale
wholesale
china wholesalescared about
wholesale digital cameras
wholesale clothing
wholesale furnitureif your customers
wholesale atv
wholesale jewelry
wholesale handbags go direct to
wholesale shoes
wedding dresses
MP3 player us whereas
wholesale mp3
wholesale t shirts
mp4 watchesnot you?
psp accessories
wholesale digital cameras
wholesale electronicsYes,sure,
wholesale shoes
wholesale mp3everbody
skin care products
wholesale mobile phones
wholesale silver jewelryis scared.
OK!Don’gps devices
Men’s outerwear
Women’s outerweart be anxious
Mens outerwear
MP4 player
wedding dress about this
Memory Card
Memory Stick
digital camera wholesalepoint.As consumer electronics
mp5 player
memory cardsa good reputationCar video players
gps devices
bluetooth headset wholesale dropshipper,wholesale clothing
wholesale furniture
Wholesale jewelryHohotrade never Ceramic tile
contact with your customers directly,the customers belong to you will just only contact with you,not with us or the other suppliers.The products will be sent to your customer without our name,address,contact details.All these information are not visible to your customers.They will be “blind” at that moment
Wholesale jewelry
Your repeated customers will be still loyal to you,they don’t need to know that Hohotrade.com is your closed and secret wholesale dropship supplier!
Dropshipping on Hohotrade,not only saves the time for you to solve the boiling water,but even helps you to keep the tremendous profit or benefit and your own customers.Come on,it is not ideal,but attainable!
wow gold
wow gold
wow goldwow goldwow gold Age of Conan gold cheap Ffxi gil to our loyal reliable customers.
wow gold
world of warcraft goldbuy wow goldcheap wow goldaoc goldwow power levelingwow powerlevelingwow levelingworld of warcraft goldbuy wow goldcheap wow goldwow power levelingwow powerlevelingbuy wow goldcheap wow goldwow power levelingwow powerlevelingrunescape goldrunescape moneyEverQuest 2 goldeq2 platEverQuest 2 goldeq2 platMaple Story Mesos maplestory MesosMaple Story meso maplestory meso Final Fantasy XI gilffxi gilLOTRO GoldLOTR Goldlord of the ring goldGranado Espada VisGranado Espada goldGE moneydofus kamaskamas dofusrunescape goldrunescape moneyflyff penyabuy flyff goldflyff moneyEverQuest platbuy eq goldEverQuest goldeq plateq goldAge of Conan goldage of conan power levelingfinal fantasy xi gilffxi gildofus kamaskamas dofusLOTRO GoldLOTR Goldlord of the ring goldsilkroad goldbuy silkroad goldAge of Conan goldage of conan power levelingmaple story mesosmaplestory mesosmaple story mesoffxi gilwow levelingwow power levelingwow powerlevelingwow goldwow power levelingwow powerlevelinghero online goldimvu creditskal geonknight goldlast chaos goldlineage adenalineage 2 adenalord of the rings goldlotro goldlotr goldmaple story mesosmaplestory mesosmaplestory mesoperfect world goldpirates of the burning Sea goldaoc goldaoc goldaoc goldAge of Conan goldaoc power levelingaoc levelingaoc goldWarhammer goldaoc power levelingaoc levelingfinal fantasy xi gilfinal fantasy 11 cheap gilfinal fantasy 11 cheap gilBuy Warhammer goldBuy Warhammer goldcheap ffxi gilcheap ffxi gilcheap ffxi gilwow goldWarhammer goldWarhammer goldBuy Warhammer goldBuy Warhammer goldWarhammer goldWarhammer goldBuy Warhammer goldBuy Warhammer goldWarhammer goldLOTRO GoldWarhammer goldWarhammer goldBuy Warhammer goldBuy Warhammer gold
learn
I am a novice, to learn the
Please revise for Drupal 6
First, I’m so sorry for acting on such an old post. However since Drupal 6, it is still being difficult to move the user picture out of the settings tab. I’ve tried for hours to get this working for Drupal 6. The validation has changed as well as user_edit_form has gone through some minor changes.
Could anyway please help modify this code for Drupal 6 as the settings page is being cramped by other modules making the user picture upload form very inaccessible.
Regards,
Marius
can I do this on a node page?
I want an anonymous user to be able to create a page and have there pic added to the post if they want.
Great tutorial - works a
Great tutorial - works a treat and solved a number of other problems too.
What i’d like to do is put the picture upload tab somewhere else completely, i’m using bio module and would like to put it as a sub tab of that module - any idea how to do that.
Yes thats setup in
Yes thats setup in drupal’s .htaccess file
yes, you can’t
yes, you can’t download.module files…
this is a restriction setup in drupal’s .htaccess file
Hi. I like this extension. I
Hi. I like this extension. I had created version for Drupal 5. Here is it: http://www.fem.uniag.sk/havran/.files/avatar-5.x.tar.gz. Thank you for sharing.
permission granted!
Haha… Drupal’s being smart. It thinks you’re trying to get at my code. I’ve replaced that with a gzip version.
And I do intend to check this into cvs, but first I’m going to add a few features.
Drupal.org project?
Cool stuff! Do you intend to always host the module here, or can we look forward to a Drupal.org project for it? Also, I missed whether this is a 4.7 or 5.0 module? Great work =)
permission denied ...
http://www.chapterthreellc.com/files/avatar.module
Forbidden
You don’t have permission to access /files/avatar.module on this server.
:)
Getting the same problem.
Getting the same problem. What’s the solution?
Yes, thats set in drupal’s
Yes, thats set in drupal’s .htaccess file
Post new comment