Hello,
I found a solution for overriding the POST rest method, by writing a class which extend EntityResource.(The post method has only one parameter which is initialized.)
I also need a solution for PATCH method to override the existing one from the core(The patch method has 2 parameters, first is "EntityInterface $entity which is not initilized and the second is initialized). When I try to override I received the following error occoured:
Recoverable fatal error: Argument 1 passed to Drupal\mymodule\Plugin\rest\resource\MyCustomEntityResource::patch() must implement interface Drupal\Core\Entity\EntityInterface, string given"
Can anyone tell me how to pass the first argument to implement the interface EntityInterface?
My patch function looks:
public function patch($entity , EntityInterface $entity = NULL ) {
$response = parent::patch($this->entityType, $entity);
return new ResourceResponse(array('Test' => 'Test'));
}
Thank you in advance!
Comments
Comment #2
iuana commentedComment #3
dawehnerThis looks like invalid code, given you have two parameters named $entity.
Do you mind trying to change the signature to
public function patch(EntityInterface $original_entity, EntityInterface $entity = NULL) {?Comment #4
iuana commentedI tried this way
public function patch(EntityInterface $original_entity, EntityInterface $entity = NULL)
The same error. Should I initialiaze first argument somewhere?
Comment #5
dawehnerDo you mind maybe sharing a bit more of your code? There might be a different problem causing that. Ideally you provide a way for someone to just download something to try out without any additional effort :)
Comment #6
imyaro commentedWhy you cannot extend other class? Not entity response?
Comment #7
iuana commentedComment #8
iuana commentedI adapt the code from the blog post: https://drupedia.org/blog/development/creating-custom-rest-resource-exis... for Patch.
My code looks:
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\rest\Plugin\rest\resource\EntityResource;
/**
* @RestResource(
* id = "my_custom_entity_resource",
* label = @Translation("My custom entity resource"),
* entity_type = "node",
* uri_paths = {
* "canonical" = "/api/my-custom-entity/{my_custom_entity_id}",
* "https://www.drupal.org/link-relations/create" = "/api/create/my-custom-entity"
* }
* )
*/
class MyCustomEntityResource extends EntityResource {
public function patch(EntityInterfce $original_entity, EntityInterface $entity= NULL) {
// Let parent create new entity.
$response = parent::patch($original_entity, $entity);
return new ResourceResponse($entity, 201);
}
}
I enabled the rest api and tried in Postman to achieve the base response without any modification.
The url used: Site/api/my-custom-entity/{id}?_format=json , {id} replaced with a node id.
Thanks
Ps. I don't need a custom rest resource, just to override the basic one.
Comment #9
wim leersIf you use
/api/my-custom-entity/{my_custom_entity_id}as the actual URL, then there will be no upcasting from an entity ID to an actual entity object.If you want to use that URL, then you should have this signature:
The article https://drupedia.org/blog/development/creating-custom-rest-resource-exis... is pretty misleading about this.
Comment #10
wim leers