I'm trying to get the avatar (profile picture) located in the $profile array to appear in a BLOCK. The variable $profile is not accessible from blocks. It's scope is only in that actual user-profile.tpl.php file. So... does anybody know how I can execute something like this:
print $profile[user_picture];
in a drupal BLOCK?
I figured i might as well post it here as well. See my second comment on the first thread in this discussion. Below is my code I used with INSERT VIEW to get what I wanted:
<?php
$profileUser = "";
if (arg(0) == "user") {
$profileUser = arg(1);
}
// removed some other checks i do to populate $profileUser
?>
[view:VIEWED_PROFILE_AVATAR=block=<?php print $profileUser; ?>]
I hope that helps someone.
You can try using the following code in a new block (admin/build/block/add):
<?php
global $user;
$output = theme_image($user->picture, $alt = 'user pic', $title = 'user pic');
print $output;
This gives you access to the global $user variable and then you can use the picture property to get the URL for the current users profile picture.
Related
I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.
Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Please change last line of your code to this:
$raised_by=ucwords($user->roles[0]);
So that your current code which display First Name i.e.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
Above code will become:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=ucwords($user->roles[0]);
}
Update: To remove underscore with space your code may become as:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
}
You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.
Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.
Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.
You could use this line of code.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = implode(', ', $user_info->roles);
}
Or, if you prefer to use the get_user_role function that you've written,
slightly modify it to take the user ID as input and return the user role.
function get_user_role($user_id) {
$user_info = get_userdata($user_id);
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You could use it like as shown below to output the user role.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = get_user_role($user->ID);
}
After reading the documentation, it seems that the correct way of doing that is like this:
<?php print $user_picture; ?>
Wich won´t work (it doesn´t print anything at all).
After asking around, I get this answer, which sadly won't work either:
<?php global $user;
$image = theme('user_picture', $user);
print $image;?>
That code was suggested as a "last resource", but when I try to use it, I get this error message:
Fatal error: Cannot use object of type stdClass as array in /includes/theme.inc on line 1054
Looking for that particular line: if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {, and in context:
if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
$element = $variables;
$variables = array();
if (isset($info['variables'])) {
foreach (array_keys($info['variables']) as $name) {
if (isset($element["#$name"])) {
$variables[$name] = $element["#$name"];
}
}
}
It's pretty clear that it should work, but it doesn't.
Using contemplate module, I get this output as an alternative:
<?php print $node->picture ?>
Which won't work either.
I've tried then with Devel module (using the /devel tab of the node):
I get this:
picture (String, 4 characters ) 2876
$...->picture
Which won't work, because the output is just "2876", but is the only "picture" there.
So what can I do? How can I trace this problem?
note that
theme('user_picture', $vars);
is a tpl.php file.
you can dpm($vars) of a preprocess function for this theme function and see what you can use.
As per node.tpl.php topic in Drupal docs, $user_picture is a valid variable in node.tpl.php file (and its variations like node--book.tpl.php)
This code is wrong:
<?php global $user;
$image = theme('user_picture', $user);
print $image;?>
it's about displaying picture of current user, and it's not for Drupal 7 AFAIK.
Try dsm($content) or dsm($node) in node.tpl.phptofind the correct variable if $user_picture is still not working for you.
I'm attempting to use the code below to display the 'Registration Information' group of my users profiles in Drupal 6. When this code is executed, the field labels from the profile form are displayed, but the input fields are not. The page source doesn't have the form tags. This code works on other installations of D6 - so I'm confident it works. Any ideas where to begin to debug?
global $user;
$uid = $user->uid;
if ($uid > 0) {
include_once drupal_get_path('module', 'user') . '/user.pages.inc';
$profile = profile_load_profile($user);
print(drupal_get_form('user_profile_form', $user, 'Registration Information'));
}
It looks like your using profile_load_profile() function, but then your not using that profile in the next line. Instead your using the global $user. I am guessing here, but aren't you loading profile because you need it in the next line?
So instead of:
print(drupal_get_form('user_profile_form', $user, 'Registration Information'));
Try:
print(drupal_get_form('user_profile_form', $profile, 'Registration Information'));
Thanks all. The only thing that ended up working was:
global $user;
module_load_include('inc', 'user', 'user.pages');
print user_edit($user, 'Registration Information');
This must be an easy one but I can't find documentation for it online.
I'm trying to use the l() function in Drupal to create a dynamic link. What's the syntax?
At the moment I have:
l('Destination',"path/$user->uid/category")
which points to:
path/%2Fcategory
first of all, if you're working within a function, you'll need to get access to the global user object.
Secondly, if the user is anonymous/not logged in, the $user->uid might not be set or be 0.
lastly to prevent errors, it is common to concatenate variables together with strings
global $user;
if ($user->uid)
{
l('Destination', 'path/'.$user->uid.'/category')
}
l() is correcting your URL to path/%2Fcategory because it's trying to make a workable link from the string path//category.
Your string is path//category because $user->uid has no value. It has no value because either you haven't pulled up a user object from global $user or user_load(), or your user is anonymous.
I would suggest putting checking the value of $user before calling l(), for example:
global $user; // or $user = user_load($foo);
if ($user) {
l('Destination', 'path/'.$user->uid.'/category');
} else {
l('Destination', 'path/you-are-not-logged-in');
}
Try concatenating the strings instead.
l('Destination',"path/".$user->uid."/category")
as for the documentation, here it is: http://api.drupal.org/api/function/l/4.7
l($text,
$path,
$attributes = array(),
$query = NULL,
$fragment = NULL,
$absolute = FALSE,
$html = FALSE)
The documentation for the l() function is located at:
http://api.drupal.org/api/function/l/6
Other stuff has been said yet by others :)
I originally started this question in another thread, but that thread was sorta, kinda answered, and now I primarily want to know how to specify another form action... I tried using the code below, but the form action, when output, remains unchanged, although looking at the print_r($form), it's correctly changed... Why isn't it picking up?
function mytheme_user_profile_form($form) {
global $user;
$uid = $user->uid;
//print '<pre>'; print_r($form); print '</pre>';
$category = $form['_category']['#value'];
switch($category) {
case 'account':
$form['#action'] = '/user/'.$uid.'/edit?destination=user/'.$uid;
break;
case 'education':
$form['#action'] = '/user/'.$uid.'/edit/education?destination=user/'.$uid;
break;
case 'experience':
$form['#action'] = '/user/'.$uid.'/edit/experience?destination=user/'.$uid;
break;
case 'publications':
$form['#action'] = '/user/'.$uid.'/edit/publications?destination=user/'.$uid;
break;
case 'conflicts':
$form['#action'] = '/user/'.$uid.'/edit/conflicts?destination=user/'.$uid;
break;
}
//print '<pre>'; print_r($form); print '</pre>';
//print $form['#action'];
$output .= drupal_render($form);
return $output;
hook_form_alter() is likely the way to go.
Here are some hopefully helpful links:
Form Theming: How do I set $form['action']?
Modifying Forms in Drupal 5 and 6
hook_form_alter
EDIT: reply to comment #1 below:
How to implement hook_form_alter():
You must create a module (you cannot use template.php). It's easier than it looks.
For a module named "formstuff", you would create formstuff.info and formstuff.module and put them in either sites/all/modules or sites/yoursitename/modules. Set up the .info and .module files per the instructions, then just create the following function in your .module file:
function formstuff_form_alter(&$form, $form_state, $form_id) {
// do stuff
}
This function is a hook because it is named properly (i.e. replace the word 'hook' with the name of your module), and it matches hook_form_alter's function signature (i.e. it takes the same parameters).
Then just enable your module in your site's admin and the hook should do it's magic.
Note that hook_form_alter takes a reference to the form; this allows you to modify it in-place.
You need to put the form_alter function in a module and then use either if or switch to check the form ID. If the form ID is the one you want to alter then give the form an action property
$form['someID'] = array(
'#action' => 'path/you/want',
);