add information to $user object

adrian.mar - July 15, 2008 - 17:09

Actually Im developing a system for reports management wich needs profile information in many system fases, for that Im using hook_user becouse when user is login in it loads some information in $user object, what I have is something like this, but Ive done test with direct information and dont happen nothing, somewhere can tell me whats wrong or how I can do this???? please

function squad_user($op, &$edit, &$account, $category = NULL) {
switch($op) {
case 'load':
$account->puesto = "EL NOMBRE DEL PUESTO (profile_puesto)";
$account->departamento = "EL NOMBRE DEL DEPARTAMENTO (profile_departamento)";
break;
}
}

help please

adrian.mar - July 16, 2008 - 00:29

somebody cab help me ...

At first glance, that code

lyricnz - July 16, 2008 - 01:04

At first glance, that code looks fine. Some questions, and suggestions:

- which version of drupal?

- is your method being called at all? Suggest add: drupal_set_message("called squad_user($op)")

- how are you testing whether the information is being successfully added to $user? Suggest: print_r($user) in a block or similar. You may need to add "global $user" to make it visible first.

Answers

adrian.mar - July 16, 2008 - 16:02

- The version of drupal is 5.7

- I done a print of my parameter using drupal_set_message and it looks right

- I have a content type named squad, and in its form "function squad_form(&$node)" I take the $user object information for display, but, dont appear $user->puesto and $user->departament, I suppose that this infomation was loaded on "function squad_user($op)"

I leave the code,

---------------------------------------------------------------------------------------------------------------------------

<?PHP
/**
* Implementation of hook_form().
*/
function squad_form(&$node) {
        $type = node_get_types('type', $node);
global $user;
print_r($user); // ---------------------------- Dont appear "nombre_completo", "puesto", "departamento"

$form['header'] = array(
'#type' => 'fieldset',
'#title' => t('Informacion de la persona que emite el reporte.'),
'#weight' => -5,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['header']['nombre_completo'] = array(
'#type' => 'textfield',
'#title' => t('Nombre completo'),
'#default_value' => $user->nombre_completo, // -------------Here
'#size' => 45,
'#requiered' => FALSE,
'#attributes' => array('class' => 'squad_trans_field'),
);
$form['header']['puesto'] = array(
'#type' => 'textfield',
'#title' => t('Puesto'),
'#default_value' => $user->puesto, // -----------------------Here
'#size' => 70,
'#requiered' => false,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
);
$form['descripcion'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#required' => FALSE
);

  return $form;
}


/**
* Implementation of hook_user().
*/
function squad_user($op, &$edit, &$user, $category = NULL) {
switch($op) {
case 'load':
/* drupal_set_message( $user->name );*/
                        $user->nombre_completo = "COMPLETE NAME (profile_firstname, profile_lastname)";
$user->puesto = "EL NOMBRE DEL PUESTO (profile_puesto)";
$user->departamento = "EL NOMBRE DEL DEPARTAMENTO (profile_departamento)";
break;
}
}

---------------------------------------------------------------------------

Any suggest is truly grateful.

1) it's a little dangerous

lyricnz - July 17, 2008 - 02:57

1) it's a little dangerous to use $user as the parameter name here (because there is a global called $user, and it's easy to make mistakes accessing the wrong $user). I use $account as the parameter to hook_user

function adrian_user($op, &$edit, &$account, $category = NULL) {
  drupal_set_message("adrian_user($op)");
  switch ($op) {
    case 'load':
      $account->puesto = "EL NOMBRE DEL PUESTO (profile_puesto)";
      $account->departamento = "EL NOMBRE DEL DEPARTAMENTO (profile_departamento)";
      break;
  }
}

2) I wrote a module to test your code above, and had exactly the same problem! It appears that the issue is that user_load() (and hence hook_load) is not called for each page request: during a user session $user is constructed explicitly from querying {user} and {session} in the DB - so any change you make is not persisted. See http://drupal.org/node/49385 for another report.

I can only suggest that you call user_load explicitly whenever you care about this attribute. For example:

global $user;
$account = user_load(array('uid' => $user->uid));
print "<p>name = $account->name; puesto = $account->puesto; departamento = $account->departamento</p>";

Hope this helps, Simon

thanks

adrian.mar - July 17, 2008 - 23:03

thanks for your answer, it was an alternative method that is enought for my sistem :)

-------------------------------------------------------------------------------

I leave my final code if somebody has any doubt.

<?php

/**
* Implementation of hook_form().
*/
function squad_form(&$node) {   
   
$type = node_get_types('type', $node);   
   
    global
$user;
   
$account = user_load(array('uid' => $user->uid));    // Para obtener la informacion del usuario- Get object $user's information
   
   
$form['header'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Informacion de la persona que emite el reporte.'),
       
'#weight' => -5,
       
'#collapsible' => TRUE,
       
'#collapsed' => FALSE,
       
'#prefix' => '<div class="squad_titulo_form"><h2>REPORTE DE ACCIDENTES/ANOMALIAS Y/O ACCION DE MEJORA PARA EL SISTEMA DE SEGURIDAD, HIGIENE, ECOLOGIA Y CALIDAD</h2></div>',
    );
   
$form['header']['nombre_completo'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Nombre completo'),
       
'#default_value' => $user->name,
       
'#size' => 45,
       
'#requiered' => FALSE,
    );   
   
$form['header']['puesto'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Puesto'),
       
'#default_value' => $account->puesto,            // --------------------------- Puesto:
       
'#size' => 70,
       
'#requiered' => false,
    );
   
$form['header']['departamento'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Departamento'),
       
'#default_value' => $account->departamento,        // ---------------------------- Departamento:
       
'#size' => 70,
       
'#requiered' => false,
    );

   
$form['title'] = array(
       
'#type' => 'textfield',
       
'#title' => check_plain($type->title_label),
       
'#required' => TRUE,
       
'#default_value' => $node->title,
       
'#weight' => -5
   
);   
   
$form['descripcion'] = array(
       
'#type' => 'textarea',
       
'#title' => check_plain($type->body_label),
       
'#default_value' => $node->body,
       
'#required' => FALSE
   
);

   
  return
$form;
}


/**
* Implementation of hook_user().
*/
function squad_user($op, &$edit, &$account, $category = NULL) {
    switch(
$op) {
        case
'load':
           
$account->puesto "Desarrollador Web";
           
$account->departamento = "Tecnologias de la Informacion";
        break;
    }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.