php - 如何在 Drupal 8 上更新自定义用户字段

标签 php oop drupal drupal-modules drupal-8

我尝试在 Drupal 8 上更新/保存用户配置文件的自定义字段。 我很幸运能够获得值(value)观,但没有把它们救回来。

这是我的代码,有人知道为什么这不起作用吗? 我已经尝试过这些的不同变体。

 $user = \Drupal\user\Entity\User::load(1);  // Load USER of ID=1
 $user->set(‚field_user_curpage‘,38); //set my custom field = 38 !!!
 $user->save();  // save … 

这就是我加载这些字段的方式:

    $user = \Drupal\user\Entity\User::load(1);  // Load user with id = 1
    $curpage_load = $user->get(‚field_user_curpage‘); // Load custom field from User Profile
    $curpage = preg_replace(‚/[^0-9]/‚, ‚‘, $curpage_load->value);  // filter for No. only
    return $curpage;  // return output this

最佳答案

Entity Load函数返回一个静态对象。

尝试用它来加载用户。
\Drupal::entityTypeManager()->getStorage('user')->load($id);

关于php - 如何在 Drupal 8 上更新自定义用户字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469551/

相关文章:

php - ajax成功查看哪个ECHO自带

php - 如何在Excel中获取MySQL表实体

java - 现实生活中的通用类示例

drupal 7显示当前用户的用户信息

javascript - 在drupal 7中,如何在模块中添加drupal_add_js?

php - 在 PHP 中执行外部 BLAST 程序

php - 在数据库中用foreach循环保存多维数组

php - 注意 : Use of undefined constant self - assumed 'self' , 当把 property_exists 作为第一个参数时

oop - 什么是公开封装类方法的经验法则

mysql - Drupal 8 数据库安装未连接到 docker mysql 容器