I know this is probably a stupid question, but...

...besides $user, what are the "global variables" in drupal? And where can I find a list of their "attributes", i.e., the things I can put after the

"->"

sign, .......... like "uid", etc., as in...

$user->uid

Thanks.... :-)

Comments

gabriella’s picture

Take a look at the drupal handbooks > http://drupal.org/handbooks

Module developers guide > http://drupal.org/node/508
Drupals API > http://drupal.org/node/326

DriesK’s picture

Both questions (which global vars are available, and what do they contain) really depend on your site configuration, installed modules, ... There are some global vars that are always available, such as $user, but even then modules may change those vars.

The easiest way to find out which globals are available is to create a new node, select the PHP input filter, and put this in the body:

<code>
<?php
print_r($GLOBALS);
?>

This will both print the superglobals (you can recognize them easily: they are printed in capitals), as well as the global vars defined by Drupal.

Furthermore, not all (global) variables are objects. Some of them are objects, others are arrays, strings, ... For example, you'll see this for the user object:

    [user] => stdClass Object
        (
            [uid] => 1

which means that you can access the uid value with $user->uid. As another example, the $conf global is an array:

    [conf] => Array
        (
            [theme_default] => bluemarine

in which case you'll have to use $conf['theme_default'].

Maybe you want to read the chapter of the PHP manual dealing with types?

Dublin Drupaller’s picture

Hi aj,

that's not a stupid question..

the other poster is correct to point you to the handbook pages, but, in general and as a quick tip, if you're coming from a theming perspective...the global $user is usually used to gather details of the visitor to the site.

If the $user is not logged in, there's very little information available, but, if it's a logged in user, you can get a lot more info.

Have a quick gander at your user table in the database..there should be a load of columns, with titles like name, mail, last_changed etc.

The corresponding php you would use would be
<?php print $user->name; ?> for the NAME column or <?php print $user->mail; ?> for the MAIL column.

So, in non techy-speak...here's how it works (understanding this was extremely useful for me when I started working with PHPTEMPLATE)

<?php global $user; /* check to see who the visitor is */ ?>
<?php if ($user->uid) {print "the visitor has a UID, so is therefore logged in (Authenticated user)";}  ?>
<?php if (!$user->uid) {print "the visitor does not have a UID, so is therefore not logged in (anonymous user)"; ?>

Or a short snippet to print information about the current visitor looking at a page:

<?php global $user; /* check to see who the visitor is */ ?>
<?php if ($user->uid):/* if the user is logged in do this stuff */?>
UID: <?php print $user->uid ;?>
Name: <?php print $user->name ;?>
Email: <?php print $user->mail ;?>
<?php endif; /* end of is the visitor logged in stuff */?>

Those are very simplistic examples...but, when I twigged to what that meant..it was a massive help for me understanding how to theme to the max using Drupal. I knew (and arguably still do) nothing about php and putting little php commands in HTML before.

To get even more information, you can use profile_load_profile($account) to load information from the PROFILE_VALUES table...and so on..

I hope that makes sense as a quick overview. Best to look at the handbook for more detailed info.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

cmsproducer’s picture

I just ready about this in the documentation api.drupal.org as I was looking for a way to find out if a commenter is in a given role so as to theme it appropriately. I think it is a natural next step.
The function user_load() allows you to supply a UID and get back an entire array of a user's data. As such, user_load($c_uid);
will provide every bit of info about the matching user.

To avoid repeating, you can read my post http://www.idonny.com/analysis/drupal/comment-author-roles

ajwwong’s picture

Thanks Everyone! I'll definitely try pounding my head through the handbooks again (it's pretty tough going in there! :-) ) and take a look at the PHP Types Chapter. Whew!

In fact, I probably need to learn a heck of a lot more PHP ... if I want to get serious about this...

But thanks, also, for the specific notes, pointing me in the right direction... I'll definitely try creating the new node to look at the globals (Dries) and then also look at the database to find attributes for "user" (Dublin Drupaller) :-) ... Whew!! It's one heck of a steep curve here!! :-)

See ya round!

Albert

Dublin Drupaller’s picture

then also look at the database to find attributes for "user" (Dublin Drupaller) :-) ... Whew!! It's one heck of a steep curve here

I'm working on a handbook section titled "A THEMERS guide to to using PHP with Drupal" which is intended for people who know how to do a little bit of HTML and is specific for Drupal Theming. Someone else is working on a more in-depth handbook for programmers.

I'll be adding it to the handbook over the next few weeks..so it will be easier for newbies to get their heads around power theming with Drupal..without being daunted by the "steep curve". If I'm able to get to grips with it. Anyone can.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

socialnicheguru’s picture

subscribing

http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.

Dr_Ernst’s picture

Obviosly there is something who change a body class to section-taxonomy so there must be a variable witch changes somewhere in the globals but I have went through them all (with doing

 < PRE > print_r($GLOBALS);  <  / PRE >

nothing recembles any information that could output to the body class: section-taxonomy

abaddon’s picture

this will help a lot with easier development, instead of print_r(), var_dump(), etc, install the devel module http://drupal.org/project/devel and use dpm($VAR_NAME); it will pretty print it and allow you to _browse more easily_ through variables, so for the first reply use this instead -> dpm($GLOBALS);