drupal-----where are these variable declared? - drupal

i saw some module using eg:
function statistics_exit() {
global $user, $recent_activity;
.......
}
where are these variables($user, $recent_activity) declared? thank you. how should i know the values of there variables.

$user is a global var which represents the current logged in user.
for more info about the Drupal core globals visit http://api.drupal.org/api/drupal/globals/6

The global $user object is first set in the sess_read() function in session.inc. I don't know where $recent_activity is set. If you want to know the value, you can simply print the variable like this:
<?php
global $user, $recent_activity;
var_dump($user, $recent_activity);
?>
Or if you have devel module installed:
<?php
global $user, $recent_activity;
dsm($user);
dsm($recent_activity);
?>

Related

Will get_current_user_id() in wordpress fire a database request?

I am using get_current_user_id() and wp_get_current_user() multiple times in my plugin. Are these functions going to send a database request everytime I use them or is the user object of the current user always available (cause wordpress requests it anyway on initializing)?
Would it be better to declare a global var at the start of my script with the current user info?
<?php
global $currentUser;
$currentUser = wp_get_current_user();
function function1() {
global $currentUser;
echo $currentUser->ID;
}
?>
No it will not. It will read the wordpress global variable $current_user

Can't get the author's picture printed onto a node in Drupal 7

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.

Drupal 7 global $user variable

Would appreciate it, if anyone can let me know how we can set the global $user variable, so that we don't have to keep re-declaring it in each function, to access its contents. How can we declare it so that all the functions in a module can use it?
The type of global you're looking for (available always, in every scope) is called a superglobal in PHP. You cannot declare new superglobals, but you can access all globals directly because they are part of the $GLOBAL superglobal. In other words, you can use $GLOBALS['user'] to access the $user global directly.
See also create superglobal variables in php? for more info and alternative methods.
You can't...that's how globals work in PHP. If you want to import the global variable into your local function then you have to use the global keyword, there's no way round it. This is a 'feature' of the PHP language, it has nothing to do with Drupal.
An alternative method might be to implement a helper function:
function get_current_user() {
global $user;
return $user;
}
And call it like this:
$user = &get_current_user();
In your function if you want to use the $user variable, you just required to use/instantiate 'global' keyword before $user it. So that you can access all the data for that current user of the website.
For example
function myGenericFunc(){
global $user;
$user_id = $user->uid;
}
Note that you cannot redeclare it.
I hope it helps.

Drupal: Access $profile from a block

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.

Check for role in node-type.tpl.php

Is there any way to check the role of the viewer within a drupal theme file, so as to write a conditional statement?
Thanks...
The current user is always available as a global variable, so just do:
// Make the user object available
global $user;
// Grab the user roles
$roles = $user->roles;
$user->roles will be an array of role names, keyed by role id (rid).
Edit: To be precise, the global user object is made available during early bootstrapping, in phase DRUPAL_BOOTSTRAP_SESSION, but from the point of custom coding within themes or modules, you can treat that global as always available.
This will do
global $user;
$num_roles = db_fetch_object(pager_query(db_rewrite_sql('SELECT rid from {role} ORDER BY rid Desc')))->rid; // Find how many roles are there
for($i=0; $i < $num_roles; $i++){
if(strlen($user->roles[$i]) >0){
echo $user->roles[$i];
$i = $num_roles;
}
}
Just an appendix to Henrik Opel's answer:
if you use it in a tpl.php file, then create a variable first in a preprocess_node function:
<?php
function YOURTEMPLATE_preprocess_node(&$variables) {
global $user;
$variables['current_user_roles'] = $user->roles;
}
?>
Now you can print your roles in your tpl.php:
<?php
if ($current_user_roles) {
?>
<ul class="roles">
<?php
foreach ($current_user_roles as $role) {
?><li class="roles-item"><?php print $role; ?></li><?php
}
?>
</ul>

Resources