You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Drupal 7自定义Housing Schemes服务添加CRUD功能?

Got it, let's expand your existing Housing Schemes REST API to include full CRUD functionality. Here's a step-by-step implementation that fits seamlessly with your current setup:


Step 1: Update Resource Definitions (housing_schemes.services.inc)

First, we need to register the Create, Update, and Delete operations alongside your existing Retrieve endpoint. Replace your current housing_schemes_services_resource() function with this:

function housing_schemes_services_resource() {
  $api = array(
    'housing_schemes' => array(
      'operations' => array(
        // Existing Retrieve operation
        'retrieve' => array(
          'help' => 'Retrieves housing schemes info.',
          'file' => array(
            'type' => 'inc',
            'module' => 'housing_schemes_services',
            'name' => 'housing_schemes.services',
          ),
          'callback' => 'housing_schemes_services_resource_retrieve',
          'access callback' => 'user_access',
          'access arguments' => array('access content'),
          'access arguments append' => FALSE,
          'args' => array(
            array(
              'name' => 'nid',
              'type' => 'int',
              'description' => 'Node ID of the housing scheme',
              'source' => array('path' => '0'),
              'optional' => FALSE,
            ),
          ),
        ),
        // New Create operation
        'create' => array(
          'help' => 'Creates a new housing scheme.',
          'file' => array(
            'type' => 'inc',
            'module' => 'housing_schemes_services',
            'name' => 'housing_schemes.services',
          ),
          'callback' => 'housing_schemes_services_resource_create',
          'access callback' => 'user_access',
          'access arguments' => array('create housing_scheme content'),
          'access arguments append' => FALSE,
          'args' => array(
            array(
              'name' => 'node_data',
              'type' => 'struct',
              'description' => 'Housing scheme data to create',
              'source' => 'data',
              'optional' => FALSE,
            ),
          ),
        ),
        // New Update operation
        'update' => array(
          'help' => 'Updates an existing housing scheme.',
          'file' => array(
            'type' => 'inc',
            'module' => 'housing_schemes_services',
            'name' => 'housing_schemes.services',
          ),
          'callback' => 'housing_schemes_services_resource_update',
          'access callback' => 'housing_schemes_services_access_update',
          'access arguments' => array(1), // Passes the node object to access callback
          'access arguments append' => TRUE,
          'args' => array(
            array(
              'name' => 'nid',
              'type' => 'int',
              'description' => 'Node ID of the housing scheme to update',
              'source' => array('path' => '0'),
              'optional' => FALSE,
            ),
            array(
              'name' => 'node_data',
              'type' => 'struct',
              'description' => 'Updated housing scheme data',
              'source' => 'data',
              'optional' => FALSE,
            ),
          ),
        ),
        // New Delete operation
        'delete' => array(
          'help' => 'Deletes an existing housing scheme.',
          'file' => array(
            'type' => 'inc',
            'module' => 'housing_schemes_services',
            'name' => 'housing_schemes.services',
          ),
          'callback' => 'housing_schemes_services_resource_delete',
          'access callback' => 'housing_schemes_services_access_delete',
          'access arguments' => array(1), // Passes the node object to access callback
          'access arguments append' => TRUE,
          'args' => array(
            array(
              'name' => 'nid',
              'type' => 'int',
              'description' => 'Node ID of the housing scheme to delete',
              'source' => array('path' => '0'),
              'optional' => FALSE,
            ),
          ),
        ),
      ),
    ),
  );
  return $api;
}

Step 2: Implement CRUD Callback Functions (housing_schemes_services.module)

Add these new functions to your module file to handle the actual Create, Update, and Delete logic:

Create Callback

function housing_schemes_services_resource_create($node_data) {
  // Validate we're only creating housing_scheme nodes
  if ($node_data['type'] !== 'housing_scheme') {
    return services_error('Invalid node type. Only "housing_scheme" is allowed.', 400);
  }

  // Initialize new node object
  $node = new stdClass();
  $node->type = 'housing_scheme';
  node_object_prepare($node);

  // Map incoming data to node fields
  $node->title = $node_data['scheme_name'];
  $node->body[LANGUAGE_NONE][0]['value'] = $node_data['about_scheme'];
  $node->field_distance_from_the_city[LANGUAGE_NONE][0]['value'] = $node_data['distance_from_the_city'];
  $node->field_nearest_major_city[LANGUAGE_NONE][0]['value'] = $node_data['nearest_major_city'];
  $node->field_locality_review[LANGUAGE_NONE][0]['value'] = $node_data['locality_review'];

  // Set default values (adjust based on your needs)
  $node->uid = isset($node_data['uid']) ? $node_data['uid'] : 1; // Admin as default author
  $node->status = isset($node_data['status']) ? $node_data['status'] : 1; // Published by default
  $node->created = REQUEST_TIME;

  // Save the node
  node_save($node);

  // Return the created node using your existing retrieve logic
  return housing_schemes_services_resource_retrieve($node->nid);
}

Update Callback

function housing_schemes_services_resource_update($nid, $node_data) {
  // Load the existing node
  $node = node_load($nid);
  if (!$node || $node->type !== 'housing_scheme') {
    return services_error('Housing scheme not found.', 404);
  }

  // Update fields with incoming data (only update provided fields)
  if (isset($node_data['scheme_name'])) {
    $node->title = $node_data['scheme_name'];
  }
  if (isset($node_data['about_scheme'])) {
    $node->body[LANGUAGE_NONE][0]['value'] = $node_data['about_scheme'];
  }
  if (isset($node_data['distance_from_the_city'])) {
    $node->field_distance_from_the_city[LANGUAGE_NONE][0]['value'] = $node_data['distance_from_the_city'];
  }
  if (isset($node_data['nearest_major_city'])) {
    $node->field_nearest_major_city[LANGUAGE_NONE][0]['value'] = $node_data['nearest_major_city'];
  }
  if (isset($node_data['locality_review'])) {
    $node->field_locality_review[LANGUAGE_NONE][0]['value'] = $node_data['locality_review'];
  }
  if (isset($node_data['status'])) {
    $node->status = $node_data['status'];
  }

  // Save the updated node
  node_save($node);

  // Return the updated node
  return housing_schemes_services_resource_retrieve($node->nid);
}

Delete Callback

function housing_schemes_services_resource_delete($nid) {
  // Load the node
  $node = node_load($nid);
  if (!$node || $node->type !== 'housing_scheme') {
    return services_error('Housing scheme not found.', 404);
  }

  // Delete the node
  node_delete($nid);

  // Return success response
  return array(
    'success' => TRUE,
    'message' => "Housing scheme with NID $nid has been deleted."
  );
}

Access Control Helpers

Add these to handle proper permission checks for Update/Delete:

function housing_schemes_services_access_update($node) {
  // Check if user can edit this specific housing scheme node
  return node_access('update', $node);
}

function housing_schemes_services_access_delete($node) {
  // Check if user can delete this specific housing scheme node
  return node_access('delete', $node);
}

Step 3: Test the New Endpoints

After clearing Drupal's cache, you can test each operation with tools like Postman or curl:

  • Create: POST to http://localhost/drurest/cf_api/housing_schemes.json with a JSON body like:
    {
      "type": "housing_scheme",
      "scheme_name": "New Downtown Scheme",
      "about_scheme": "Modern apartments in the city center",
      "distance_from_the_city": "2 km",
      "nearest_major_city": "Austin",
      "locality_review": "Excellent"
    }
    
  • Update: PUT to http://localhost/drurest/cf_api/housing_schemes/{nid}.json with partial or full node data
  • Delete: DELETE to http://localhost/drurest/cf_api/housing_schemes/{nid}.json

内容的提问来源于stack exchange,提问作者Niladri Banerjee - Uttarpara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:36:11