我尝试在 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/