I am a beginning Drupal developer and wanted to share my greatest frustration to the community in the hopes that there is an answer out there.

I have searched the handbook pages, api pages, and forums - and while I HAVE found scattered, fragmented, and inconsistent bits of information here and there, I have NOT yet found a complete or formal list of global php variables that would be available in a core installation.

Am I missing something?

My first stop was the API pages (http://api.drupal.org/api/globals) - I have already looked it over and found it difficult to use and/or trust for the the following reasons:

1. The list seems incomplete. For example, there are a ton of variables listed in the comments block of the page.tpl.php, only half of which are not listed on the api page. Still, there are many vars on the api page which are not listed in the comments of that tpl page. What is missing from the api page and why?

2. All or most of the global vars on the api page have little or absolutely no information about the var, other than the name (for example, $user). At the very least I would expect to see what type of object would get returned, or (even better) a list of properties/methods/etc. Perhaps this is just my ignorance of php speaking - but how am I to know what to DO with the $user var? For example how do I get the uuid from it? Are there other things I can get from it as well? I would expect this kind of information to be in the api docs. (Please don't just respond and tell me how to get the uuid from $user because thats not what I'm asking here)

I understand that these globals can be modified by other modules, and that custom modules can introduce new global vars that could not possibly be anticipated. I also understand that I could probably dissect the source code and/or learn mysql and start doing database queries and possibly ASSUME what variables might be available and how they might be used - but what I am looking for here is some clear and solid api documentation for a D6 Core/Default installation - does it exist or am I wasting my time looking?

-Thanks in advance-

Comments

livingegg’s picture

bump

livingegg’s picture

bump

livingegg’s picture

... the silence is deafening ...

michelle’s picture

Well, you've already found and discounted the official list so it's not likely anyone else is going to answer no matter how many times you bump it.

Documentation can be contributed by anyone. If you've found problems, feel free to help out. :)

Michelle

livingegg’s picture

Michelle - do you know a way to do something like, print out all the available properties of $user (for example), and what information they provide? Tell me how to do that and I'll gladly document the properties of every var on this page: http://api.drupal.org/api/globals (where applicable). Otherwise - I think think what you're saying to me is double-bind.

michelle’s picture

If you have devel installed, you can just put dsm($user); at the point in your code that you need to use it. Keep in mind that what's there depends on what contrib modules you have installed so only core properties should be documented on that page.

Michelle

Openlogic’s picture

There's a list of global vars on api.drupal.org here:
http://api.drupal.org/api/globals

livingegg’s picture

Brilliant!

alan d.’s picture

I think you need to see what a true global is! The variables listed in the templates are not (normally) globals, and the list of defined variables can be added to by every module that implements a preprocess call (there are about 2500 modules on contributions). Every theme_xxx can have a preprocess hook, as well as having the other variables added by the theme system.

Generally, the $vars array is populated by 1 or more preprocess hooks, and just before the template is included, these variables are extracted into the local scope of the function that includes the template.

Eg: This is used to include all templates

<?php
function theme_render_template($template_file, $variables) {
  extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
  ob_start();                      // Start output buffering
  include "./$template_file";      // Include the template file
  $contents = ob_get_contents();   // Get the contents of the buffer
  ob_end_clean();                  // End buffering and discard
  return $contents;                // Return the contents
}
?>

While you can search for all preprocess hooks for a given template, eg for page.tpl.php

<?php
phptemplate_preprocess_page(&$vars); # garland
template_preprocess_page(&$variables); # includes/theme.inc

?>

it is much easier to install devel and doing a dpr or dpm on the locally defined variables

<?php
  print '<pre>' . print_r(get_defined_vars(), TRUE) .'</pre>';
?>

Alan Davison
livingegg’s picture

Thanks Alan, that is some good information - so is there a way use devel to do something like print out all the properties that can be accessed on (for example) $user?

alan d.’s picture

With devel use dpr to print the $user object directly onto the page, or dpm to print the variable to the message area. The dpm is "delayed" one page load when doing this from a template as the messages are already rendered at this point (sometimes)

:)


Alan Davison
gkatsanos’s picture

Hello, This is exactly what I've been searching for quite some time, and I also could contribute. Unfortunately bad documentation is like a plague in DO.

Livingegg did you have any luck with printing all variables?

livingegg’s picture

alpapan posted this to the docs page - thanks!

http://api.drupal.org/api/global/user

yuriy.babenko’s picture

RE 1:

You can always use get_defined_vars() to see a list of all variables available in the current scope:

  echo '<pre>';
  print_r(get_defined_vars());
  echo '</pre>';

RE 2:

Just do the same print_r() call on the object you're interested in to see all of its properties.

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com