Hello,
I'm completely new to working on module development and my php knowledge is very limited.
I'm trying to modify the behavior of a custom module. I need to retrieve the drupal user id/user name information in one of the .php files but can't figure out how. I added the following snippet to the file but nothing is being written to $uid:

global $user;
$uid = $user->uid;

The module I'm trying to modify has a similar structure to fckeditor plugin. The parent directory is the drupal module/plugin and has the .module file among others. The module software folders/files resides in a sub-directory inside the parent directory. The .php file where I need the drupal user id info resides in a folder inside the module software directory.

Please ask if you need further information to help out.

n00bie

Comments

yuriy.babenko’s picture

If this file is not being called directly by drupal, but rather is being used as some sort of weird include for the module, there is a chance that the global $user variable will not be available (this would explain why your code, while perfectly valid, does not work).

Try this to see what globals are available:

echo '<pre>';
print_r($_GLOBALS);
echo '</pre>';

It would be best if you could post what module you're modifying and exactly where.

A sort-of-solution would be to use hook_user, with the 'login' case and store the user id (uid) in the session, and then retrieve it from your PHP script.
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

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

n00bie’s picture

Thanks for your reply yuriy@yuba. Looks like as you said the file isn't being directly called by drupal because print_r($_GLOBALS) doesn't print anything. So, I guess no globals are available in the .php file. You mentioned "A sort-of-solution would be to use hook_user, with the 'login' case and store the user id (uid) in the session, and then retrieve it from your PHP script". Could you please provide me an example of how to do that?

drawk’s picture

You said that you were modifying a module, but if the extension is .php, this seems like it wouldn't be the case. What 'module' are you working with?

If Drupal isn't being bootstrapped, hook_user isn't likely to be available to you either ..

n00bie’s picture

The module is wysiwygPro and is currently discontinued :(

yuriy.babenko’s picture

Create a separate module and implement hook_user() (look at it up api.drupal.org). Then, in the login case, put

$_SESSION['drupal_uid'] = $user->uid;

and in your external file:

session_start();
$uid = $_SESSION['drupal_uid'];

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

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

n00bie’s picture

Thanks, for your reply again. This looks to be beyond the scope of my PHP knowledge. Below is the code of the hooks implemented in the current .module file. It doesn't look to me that hook_user() is implemented here. Can I add code this file to implement hook_user() ?


/**
 * Implementation of hook_help
 */
function wysiwygPro_help($section = '') {

}


/**
 * Implementation of hook_perm
 */
function wysiwygPro_perm() {
  return array('use basic wysiwygPro', 'use advanced wysiwygPro', 'allow file/folder management');
}

Are you suggesting that I create another .module file in addition to the existing one to implent hook_user()?