Drupal 8.3.2에서 외부 JSON을 가져온 다음 사용자 정의 REST POST 플러그인을 작성하려고 시도했습니다.Drupal 8.3 사용자 정의 나머지 POST 오류 BadRequestHttpException : 유형 링크 관계를 지정해야합니다.
는 그 안내에 따라이 : How to create Custom Rest Resources for POST methods in Drupal 8 을 그리고 이것은 내 코드입니다 :
return new ResourceResponse(array('test'=>'OK'));
: 내가 페이로드를 전달하고이 방법으로 반환 값을 modifing에없이이를 테스트하려고하면 지금
<?php
namespace Drupal\import_json_test\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\Entity\Node;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Psr\Log\LoggerInterface;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "tio_rest_json_source",
* label = @Translation("Tio rest json source"),
* serialization_class = "Drupal\node\Entity\Node",
* uri_paths = {
* "canonical" = "/api/custom/",
* "https://www.drupal.org/link-relations/create" = "/api/custom"
* }
*)
*/
class TioRestJsonSource extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs a new TioRestJsonSource object.
*
* @param array $configuration
* A configuration array containing information about the plugin
instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id,
$plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array
$configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('import_json_test'),
$container->get('current_user')
);
}
/**
* Responds to POST requests.
*
* Returns a list of bundles for specified entity.
*
* @param $data
*
* @param $node_type
*
* @return \Drupal\rest\ResourceResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function post($node_type, $data) {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
$node = Node::create(
array(
'type' => $node_type,
'title' => $data->title->value,
'body' => [
'summary' => '',
'value' => $data->body->value,
'format' => 'full_html',
],
)
);
$node->save();
return new ResourceResponse($node);
}
}
작동 중! 심포니 \ 구성 요소 \ HttpKernel \ 예외 \의 BadRequestHttpException :
{
"title": [{
"value": "Test Article custom rest"
}],
"type": [{
"target_id": "article"
}],
"body": [{"value": "article test custom"}]
}
내가 가진 400 오류를받을 : 내가 위 내 사용자 지정 코드를 사용하여 다음과 같이 사용자 정의 페이로드를 보내는 경우
그러나 유형 링크 관계를 지정해야합니다 . Drupal \ rest \ RequestHandler-> handle() (core/modules/rest/src/RequestHandler.php의 103 행).
무엇이 잘못 되었나요?
Thx.
이/**
* Responds to POST requests.
*
* Returns a list of bundles for specified entity.
*
* @param $data
*
*
* @return \Drupal\rest\ResourceResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function post($data) {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
return new ResourceResponse(var_dump($data));
중요한 점은 다음과 같습니다
* serialization_class = "Drupal\node\Entity\Node",
그럼 난 그냥 내 게시물 함수에서 데이터를 알아서 : 나는 주석을 제거한
: