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

iuana created an issue. See original summary.

iuana’s picture

Issue summary: View changes
dawehner’s picture

public function patch($entity , EntityInterface $entity = NULL ) {
$response = parent::patch($this->entityType, $entity);
return new ResourceResponse(array('Test' => 'Test'));
}

This 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) { ?

iuana’s picture

I tried this way
public function patch(EntityInterface $original_entity, EntityInterface $entity = NULL)
The same error. Should I initialiaze first argument somewhere?

dawehner’s picture

Do 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 :)

imyaro’s picture

Why you cannot extend other class? Not entity response?

iuana’s picture

Title: Can't override the POST method » Can't override the PATCH method
iuana’s picture

I 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.

wim leers’s picture

Category: Task » Support request
Status: Active » Postponed (maintainer needs more info)
Issue tags: -rest module

If 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:

public function patch($my_custom_entity_id) {
  …
}

The article https://drupedia.org/blog/development/creating-custom-rest-resource-exis... is pretty misleading about this.

wim leers’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.