Hey,
My case is that I have a content type called profile_pic. I want users to be able to upload pics to a profile gallery, so I was thinking maybe there is a way to add an if statement where if a "do you want this to be your avatar?" checkbox is checked, then the avatar gets updated.

I assume this also means another db query so that if a user checks this box, the database would have to be updated so that no other node of this type by this user has it checked.

And also I suppose to disable the checkbox if this is the first time a user has created a node of this type...

To clarify:
1. If this is the first time a user create a "profile_pic" node - Checkbox is diabled and this image becomes user's avatar.
2. If user creates another "profile_pic" node and DOESN'T check the box, avatar does not update
3. If user creates another or UPDATES "profile_pic" node and DOES check the box, avatar updates to node's image field, and database gets updated so that no other "profile_pic" nodes by this user has the checkbox still set.

I'm still learning the drupal api and so far everything I tried to do was documented perfectly, but I'm struggling on how to do this in a drupali way. If anyone here can point me in the right direction or can think of a better way of achieving what I need, that would be very helpful!

Thanks!

Comments

gorgo’s picture

UPDATE

I managed to fix this with a custom module for my site.
Not closing this cause I'm not sure if the maintainer of this module wants to leave it as an open feature request or not.

bluecobalt’s picture

Hey gorgo,

Would you be willing to share your custom module? This is a very useful feature.

thanks,
Blue

gorgo’s picture

Sure Blue!
Actually if you use a custom module, you can disable this one.

Just create a content type with an image field and a checkbox, and a custom module with this code:


/**
 * Set image as profile pic
 **/

function MODULENAME_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
	global $user;
	switch ($op) {
    case 'delete':
		//If the node is deleted, remove avatar
    	if($node->type == 'CONTENT_TYPE' && $node->field_CHECKBOX['0']['value'] == 1){
      		db_query("UPDATE {users} SET picture='%s' WHERE uid=%d LIMIT 1", '', $user->uid);
		drupal_set_message(t('You have no main profile picture'));
  		}
      		break;
     
     case 'insert': 		
     case 'update':
    	if($node->type == 'CONTENT_TYPE' && $node->field_CHECKBOX['0']['value'] == 1){
    		
    		$avatar_path = $node->field_IMAGE['0']['filepath'];
      		db_query("UPDATE {content_type_CONTENT_TYPE} p INNER JOIN {node} n ON p.nid = n.nid SET p.field_CHECKBOX_value='%d' WHERE n.uid=%d AND p.nid !='%d'", 0, $user->uid, $node->nid);
      		db_query("UPDATE {users} SET picture='%s' WHERE uid=%d LIMIT 1", $avatar_path, $user->uid);
			drupal_set_message(t('New profile picture set'));
  		}
      		break;
  }
}


You need to change MODULENAME to your module's name, CONTENT_TYPE(where in caps) to the name of your content type, field_CHECKBOX to the name of your checkbox field and field_IMAGE to the name of your image field.

Hope this helps. I have additional conditions and functionality in mine and had to rewrite it for you, so it's untested, but this should work.

gorgo’s picture

I should also mention that the checkbox field should be an integer with 1 as the value for 'Set as profile pic (or however you wanna label it)'.

hongpong’s picture

Issue tags: +avatar

neat subscribe