【问题标题】:How to load User Profile Type in Drupal 8?如何在 Drupal 8 中加载用户配置文件类型?
【发布时间】:2017-03-30 12:50:01
【问题描述】:

我想按用户 ID 加载用户配置文件模块数据:

代码:

$profile_type = 'my_profile';
$user_id = 1;
$current_user = User::load($user_id);
$active_profile = $this->entityTypeManager()->getStorage('profile')->loadByUser($current_user, $profile_type);

print_r($active_profile);

但是,它会返回以下错误。

错误:

The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Argument 1 passed to Drupal\profile\ProfileStorage::loadByUser() must implement interface Drupal\Core\Session\AccountInterface, null given, called in C:\xampp\htdocs\catchow\htdocs\modules\custom\catchow_registration_contactlab\src\Controller\CatchowRegistrationContactlabController.php on line 122 and defined in Drupal\profile\ProfileStorage->loadByUser() (line 16 of modules\contrib\profile\src\ProfileStorage.php).
Drupal\profile\ProfileStorage->loadByUser(NULL, 'authenticated_user') (Line: 122)

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    这已经很晚了,但其他人会来这里(就像我一样)。

    我不得不进入配置文件模块源代码才能找到这个,但我想你仔细想想就很明显了:

    $list = \Drupal::entityTypeManager()
      ->getStorage('profile')
      ->loadByProperties([
        'uid' => $account->id(),
        'type' => 'profile_type',
      ]);
    

    您可能希望将其封装在服务中 - 如果您不这样做,您应该仔细审视一下自己为什么不这样做:-)

    【讨论】:

      【解决方案2】:

      您是否尝试使用user_load 函数而不是User::Load

      $current_user = user_load($user_id);
      

      https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_load/8.2.x

      当您转储$current_user 时,您有一些数据吗?

      【讨论】:

      • 是的,我正确地得到了$current_user 对象。
      • 这没有任何用处,因为配置文件字段没有附加到用户对象。
      • 第一;用户在问如何加载用户……而不是醋栗用户本身。第二; “user_load”已折旧(请参阅api.drupal.org/api/drupal/core%21modules%21user%21user.module/…
      • @sea26.2 首先:我得到他的代码来回答。第二:“将在 D9 上删除”而不是在 D8 上,因此已弃用,但在版本 8 中仍然可用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2014-11-14
      • 2022-01-05
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      相关资源
      最近更新 更多