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 REST resource in Drupal 9 for managing entities with CRUD operations using RESTful endpoints.”
// 1. Create a custom module (e.g., custom_rest_module).

// 2. Define the module in custom_rest_module.info.yml.
name: 'Custom Rest Module'
type: module
description: 'Provides a custom REST resource for managing entities.'
core_version_requirement: ^8 || ^9

// 3. Create a file custom_rest_module.routing.yml for defining routes.
custom_rest_module.entity:
  path: '/custom-rest-endpoint/{entity_id}'
  defaults:
    _controller: '\Drupal\custom_rest_module\Controller\CustomRestController::handle'
  methods:
    - 'GET'
    - 'POST'
    - 'DELETE'
    - 'PATCH'
  requirements:
    _access: 'TRUE'

// 4. Create a controller CustomRestController.php.
namespace Drupal\custom_rest_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * CustomRestController controller.
 */
class CustomRestController extends ControllerBase {

  /**
   * Handle requests for the custom REST resource.
   */
  public function handle(Request $request, $entity_id) {
    switch ($request->getMethod()) {
      case 'GET':
        // Implement GET method logic.
        $response = $this->getEntity($entity_id);
        break;

      case 'POST':
        // Implement POST method logic.
        $response = $this->createEntity($request->getContent());
        break;

      case 'DELETE':
        // Implement DELETE method logic.
        $response = $this->deleteEntity($entity_id);
        break;

      case 'PATCH':
        // Implement PATCH (update) method logic.
        $response = $this->updateEntity($entity_id, $request->getContent());
        break;

      default:
        $response = new JsonResponse(['error' => 'Invalid request method.'], 400);
    }

    return $response;
  }

  // Implement CRUD methods (getEntity, createEntity, deleteEntity, updateEntity).

  // Example:
  private function getEntity($entity_id) {
    // Implement logic to fetch entity by ID.
    // ...

    return new JsonResponse(['data' => 'Entity data for ID ' . $entity_id]);
  }

  private function createEntity($data) {
    // Implement logic to create a new entity.
    // ...

    return new JsonResponse(['message' => 'Entity created successfully.']);
  }

  private function deleteEntity($entity_id) {
    // Implement logic to delete entity by ID.
    // ...

    return new JsonResponse(['message' => 'Entity deleted successfully.']);
  }

  private function updateEntity($entity_id, $data) {
    // Implement logic to update entity by ID.
    // ...

    return new JsonResponse(['message' => 'Entity updated successfully.']);
  }

}

Remember to replace custom_rest_module with your actual module name and modify the code according to your entity type and business logic. This example provides a basic structure; you may need to enhance it based on your specific requirements.

Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner