php - Drupal 8 通过 REST API 更新用户资料图片
问题描述
我正在尝试使用 REST API 上传图像以更改 Drupal 中的用户个人资料图片。我被卡住了。这是我当前的代码:
my_module.routing.yml:
my_module.api.user.profile.update_picture:
path: '/api/user/profile/update-picture'
defaults:
_controller: \Drupal\my_module\Controller\UserProfileController::updatePicture
requirements:
_access: "TRUE"
用户配置文件控制器:
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class UserProfileController extends ControllerBase
{
protected $currentUser;
public function __construct()
{
$this->currentUser = \Drupal::currentUser();
}
public function updatePicture(Request $request)
{
$file = file_save_upload('image');
var_dump($file); die; // <----- returned false in my postman below
$user = \Drupal\user\Entity\User::load($this->currentUser->id());
$user->set('user_picture', $file->id());
$user->save();
return new JsonResponse([
'success' => FALSE,
'debug' => [
'userId' => $this->currentUser->id(),
],
]);
}
}
解决方案
推荐阅读
- date - Azure 数据工厂 - 组件“派生列”中过去 7 天的日期表达式
- matlab - 如何在 Matlab 中美化我的代码(制表符、删除不必要的空格等)?
- python - 如何提高 matplotlib 的精度?
- jquery - 用jquery ajax替换html不会改变隐藏字段值
- android - ViewModel 单元测试
- powershell - 使用 powershell 拆分列表
- testing - Spring Batch - JUnit 测试时回滚
- python - 在 Python 中使用 zstandard 压缩文件
- python - 按特定列表分组(?)数据帧
- java - 删除 Elasticsearch 中小于某个值的所有元素