“Learn to create a custom Drupal 9 REST ResourceBase Plugin for CRUD operations on nodes with detailed code and explanations.”
// Step 1: Define the custom REST ResourceBase Plugin.
/**
* @file
* Contains \Drupal\custom_rest\Plugin\rest\resource\CustomRestResource.
*/
namespace Drupal\custom_rest\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
/**
* Provides a custom REST resource for node operations.
*
* @RestResource(
* id = "custom_rest_resource",
* label = @Translation("Custom REST Resource"),
* uri_paths = {
* "canonical" = "/custom-rest/{nid}",
* "https://www.drupal.org/link-relations/create" = "/custom-rest"
* }
* )
*/
class CustomRestResource extends ResourceBase {
// Step 2: Implement the GET method.
/**
* Responds to GET requests.
*
* @param int $nid
* The node ID.
*
* @return \Drupal\rest\ResourceResponse
* The response containing node details.
*/
public function get($nid) {
// Fetch node details.
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
// Return the response.
return new ResourceResponse($node->toArray());
}
// Step 3: Implement the POST method.
/**
* Responds to POST requests.
*
* @param array $data
* The data to create a node.
*
* @return \Drupal\rest\ResourceResponse
* The response containing the created node details.
*/
public function post(array $data) {
// Create a new node.
$node = \Drupal\node\Entity\Node::create($data);
$node->save();
// Return the response.
return new ResourceResponse($node->toArray(), 201);
}
// Step 4: Implement the DELETE method.
/**
* Responds to DELETE requests.
*
* @param int $nid
* The node ID to delete.
*/
public function delete($nid) {
// Load and delete the node.
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$node->delete();
// Return a success message.
return new ResourceResponse(['message' => 'Node deleted successfully.']);
}
// Step 5: Implement the PATCH method for updating.
/**
* Responds to PATCH requests.
*
* @param int $nid
* The node ID to update.
* @param array $data
* The data to update the node.
*
* @return \Drupal\rest\ResourceResponse
* The response containing the updated node details.
*/
public function patch($nid, array $data) {
// Load and update the node.
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$node->set($data);
$node->save();
// Return the response.
return new ResourceResponse($node->toArray());
}
}Explanation:
- CustomRestResource Class: Defines the custom REST resource using the ResourceBase class.
- GET Method (Retrieve): Implements the get method to respond to GET requests, fetching node details by ID.
- POST Method (Create): Implements the post method for creating a new node based on the provided data.
- DELETE Method (Delete): Implements the delete method for deleting a node by ID.
- PATCH Method (Update): Implements the patch method for updating a node by ID with provided data.
- ResourceResponse: Utilizes ResourceResponse to format and return responses with appropriate HTTP status codes.
