With over a decade of web development experience, I specialize in Drupal (7, 8, 9, 10), CodeIgniter, Laravel, and WordPress. I offer extensive expertise in both module and theme development, providing customized solutions for complex projects. Whether you need to enhance an existing platform, create new features, or seek expert guidance, I'm here to assist. My dedication to delivering high-quality, efficient, and scalable solutions is unmatched. Feel free to contact me to explore how I can contribute to your project's success. Let's turn your ideas into reality!

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

  1. CustomRestResource Class: Defines the custom REST resource using the ResourceBase class.
  2. GET Method (Retrieve): Implements the get method to respond to GET requests, fetching node details by ID.
  3. POST Method (Create): Implements the post method for creating a new node based on the provided data.
  4. DELETE Method (Delete): Implements the delete method for deleting a node by ID.
  5. PATCH Method (Update): Implements the patch method for updating a node by ID with provided data.
  6. ResourceResponse: Utilizes ResourceResponse to format and return responses with appropriate HTTP status codes.
Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner