Drupal 7 更改用户为当前会话创建的字段值
Drupal 7 Change user created field value for current session
我对 Drupal 非常陌生。
我需要在当前会话中设置用户创建的字段值,而不必将其保存到数据库中。
我有一个网站,该网站反映了房屋建筑商协会的会员资格,这些用户多年来一直是该协会的会员,但最近才作为用户添加到该网站。因此,该站点将他们显示为短时间而不是长时间的成员。
我为他们加入协会时添加了一个自定义字段,我已经有了它,我可以在其中获取该信息并更改用户信息并将其保存到数据库中(在用户配置文件中-item.tpl.php), 但它只会更改后续数据库调用的信息,而不是当前会话的信息。我也试过更改 user-profile.tpl.php 中的 $user_profile 变量,但这也没有改变当前会话的任何内容。我不介意更改数据库,但是如何更新当前会话中创建的字段值?
网站:http://certifiedmasterbuilder.com
这是我试过的:
global $user;
$user = user_load($user_profile['field_zip']['#object']->uid);
$join_date = field_get_items('user', $user, 'field_join_date')[0]['value']; $user->created = $join_date;
$user_save($user); // <-- This saves it to the database, but does not update the //current session value
// I tried this to update the current session, but it does not work
field_attach_update('user', $user);
entity_get_controller('user')->resetCache(array($user->uid));
我通过编辑用户配置文件-item.tpl.php 使其正常工作。我得到了我们正在查看的用户配置文件,检索了我想要的字段值,检查它是否包含与用户创建日期不同的值,然后将 $value 变量更改为格式化日期:
$user = user_load(arg(1)); // Make sure the user object is fully loaded
$join_date = field_get_items('user', $user, 'field_join_date')[0]['value'];
if( $join_date > 0 && $user->created != $join_date )
{
$value = format_interval(REQUEST_TIME - $join_date);
}
我唯一的问题是除了使用 arg(1) 之外是否还有更好的方法来获取用户 ID。我听说这不是一个很好的方法。
我对 Drupal 非常陌生。
我需要在当前会话中设置用户创建的字段值,而不必将其保存到数据库中。
我有一个网站,该网站反映了房屋建筑商协会的会员资格,这些用户多年来一直是该协会的会员,但最近才作为用户添加到该网站。因此,该站点将他们显示为短时间而不是长时间的成员。
我为他们加入协会时添加了一个自定义字段,我已经有了它,我可以在其中获取该信息并更改用户信息并将其保存到数据库中(在用户配置文件中-item.tpl.php), 但它只会更改后续数据库调用的信息,而不是当前会话的信息。我也试过更改 user-profile.tpl.php 中的 $user_profile 变量,但这也没有改变当前会话的任何内容。我不介意更改数据库,但是如何更新当前会话中创建的字段值?
网站:http://certifiedmasterbuilder.com
这是我试过的:
global $user;
$user = user_load($user_profile['field_zip']['#object']->uid);
$join_date = field_get_items('user', $user, 'field_join_date')[0]['value']; $user->created = $join_date;
$user_save($user); // <-- This saves it to the database, but does not update the //current session value
// I tried this to update the current session, but it does not work
field_attach_update('user', $user);
entity_get_controller('user')->resetCache(array($user->uid));
我通过编辑用户配置文件-item.tpl.php 使其正常工作。我得到了我们正在查看的用户配置文件,检索了我想要的字段值,检查它是否包含与用户创建日期不同的值,然后将 $value 变量更改为格式化日期:
$user = user_load(arg(1)); // Make sure the user object is fully loaded
$join_date = field_get_items('user', $user, 'field_join_date')[0]['value'];
if( $join_date > 0 && $user->created != $join_date )
{
$value = format_interval(REQUEST_TIME - $join_date);
}
我唯一的问题是除了使用 arg(1) 之外是否还有更好的方法来获取用户 ID。我听说这不是一个很好的方法。