从http://valuebound.com/resources/blog/how-to-create-custom-web-services-for-drupal-7-0启发,我在D7中创建了一个rest api / webservice。我安装了所需的模块ctools,服务,图书馆,实体等,并创建了valuebound.com中提到的内容类型“住房计划”。
然后根据valuebound,我创建了一个名为“housing_schemes_services”的自定义模块。在这个文件夹中,我创建了三个名为:
的文件housing_schemes_services.info housing_schemes_services.module housing_schemes.services.inc
.info文件内容为:
name = Housing Schemes Services
description = WebService APIs for Housing Schemes.
core = 7.x
package = Housing Schemes
project = housing_schemes
dependencies[] = services
dependencies[] = rest_server
.module文件内容如下:
<?php function housing_schemes_services_services_resources() {
$resources = array();
//Include the necessary inc files.
module_load_include('inc', 'housing_schemes_services', 'housing_schemes.services');
//Service Apis for contents.
$resources += housing_schemes_services_resource();
return $resources;
}
function housing_schemes_services_resource_retrieve($nid) {
$node = node_load($nid);
//Check if the node type is housing schemes.
if ($node->type == 'housing_scheme') {
//Get PROPERTY DESCRIPTION data from field collection.
$property_description = $node->body[LANGUAGE_NONE][0]['value'];
// here we are loading field collection
$pd_item = field_collection_item_load($property_description);
$return_obj = array(
'scheme_name' => $node->title,
'scheme_id' => $node->nid,
'created_date' => $node->created,
'about_scheme'=>$node->body[LANGUAGE_NONE][0]['value'],
'distance_from_the_city'=>$node->field_distance_from_the_city[LANGUAGE_NONE][0]['value'],
'nearest_major_city' => $node->field_nearest_major_city[LANGUAGE_NONE][0]['value'],
'distance_from_the_city' => $node->field_distance_from_the_city[LANGUAGE_NONE][0]['value'],
'locality_review' =>$node->field_locality_review[LANGUAGE_NONE][0]['value'],
//'property_description' => $pd_item_values,
);
}
return $return_obj;
}
.inc文件内容为:
<?php
function housing_schemes_services_resource() {
$api = array(
'housing_schemes' => array(
'operations' => array(
'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' => 'Function to perform',
'source' => array(
'path' => '0'
),
'optional' => TRUE,
'default' => '0',
),
),
),
),
),
);
return $api;
}
当我输入http://localhost/drurest/cf_api/housing_schemes/1.json时,它工作正常,例如如下:
{"scheme_name":"House 1","scheme_id":"1","created_date":"1516180661","about_scheme":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.","distance_from_the_city":"10 km","nearest_major_city":"Dallas","locality_review":"Good"}
现在,我想通过此服务模块创建CRUD功能?我怎样才能做到这一点?比如,可以通过webservices将数据添加/更新/删除到Drupal中。寻求你的帮助。