“Learn to create a Drupal 9 Custom REST ResourceBase for CRUD operations on nodes with detailed code and sample responses.”
/**
* @file
* Contains \Drupal\custom_rest_resource\Plugin\rest\resource\CustomRestResource.
*/
namespace Drupal\custom_rest_resource\Plugin\rest\resource;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
/**
* Provides a resource to get, create, update, and delete nodes.
*
* @RestResource(
* id = "custom_rest_resource",
* label = @Translation("Custom REST Resource"),
* uri_paths = {
* "canonical" = "/custom-rest-resource/{nid}",
* "https://www.drupal.org/link-relations/create" = "/custom-rest-resource"
* }
* )
*/
class CustomRestResource extends ResourceBase {
/**
* A curent user instance.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase 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\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@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('custom_rest_resource'),
$container->get('entity_type.manager')
);
}
/**
* Responds to GET requests.
*
* Returns a node for a given NID.
*
* @param int $nid
* The node ID.
*
* @return \Drupal\rest\ResourceResponse
* The response containing the node data.
*/
public function get($nid) {
// Implementation for GET request.
// Load the node using $nid.
// Build the response array.
$response = [
'nid' => $nid,
'title' => 'Sample Node Title',
// Add more fields as needed.
];
return new ResourceResponse($response);
}
/**
* Responds to POST requests.
*
* Creates a new node.
*
* @param array $data
* The data to create the node.
*
* @return \Drupal\rest\ResourceResponse
* The response containing the newly created node data.
*/
public function post(array $data) {
// Implementation for POST request.
// Create a new node using $data.
// Build the response array.
$response = [
'nid' => 123, // ID of the newly created node.
'message' => 'Node created successfully.',
];
return new ResourceResponse($response);
}
/**
* Responds to DELETE requests.
*
* Deletes a node for a given NID.
*
* @param int $nid
* The node ID to delete.
*
* @return \Drupal\rest\ResourceResponse
* The response indicating the success of the deletion.
*/
public function delete($nid) {
// Implementation for DELETE request.
// Delete the node using $nid.
// Build the response array.
$response = [
'message' => 'Node deleted successfully.',
];
return new ResourceResponse($response);
}
/**
* Responds to PATCH requests.
*
* Updates a node for a given NID.
*
* @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 data.
*/
public function patch($nid, array $data) {
// Implementation for PATCH request.
// Update the node using $nid and $data.
// Build the response array.
$response = [
'nid' => $nid,
'message' => 'Node updated successfully.',
// Include updated fields as needed.
];
return new ResourceResponse($response);
}
}This code defines a custom REST resource in Drupal 9 for performing CRUD operations on nodes. It includes methods for GET, POST, DELETE, and PATCH requests with sample response data.
