【问题标题】:How to update custom user fields on Drupal 8如何更新 Drupal 8 上的自定义用户字段
【发布时间】:2018-09-23 19:31:19
【问题描述】:

我尝试在 Drupal 8 上更新/保存用户配置文件的自定义字段。 我很幸运获得了价值,但没有将它们保存回来。

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

 $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

【问题讨论】:

  • 可能是这样的:$edit = $user->set('field_user_curpage', 38); $user->save($user, $edit); 但也不起作用:(

标签: php oop drupal drupal-modules drupal-8


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回复。加载工作正常,但设置/保存却不行。我将加载用户行更改为您的,但我仍然无法更改/保存字段。
  • 我已经多次使用“加载”、“设置”、“保存”,它一直对我有用。这些是标准的内容实体函数,所以它应该可以工作。
  • 也许可以解释为什么您认为它不起作用。我知道在最近的工作中,我还必须设置其他内容,以便在内容修订列表中显示新修订,但我不确定这是否适用于您。
猜你喜欢
  • 2018-07-24
  • 2020-06-21
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
相关资源
最近更新 更多