0

我正在使用 REST 服务器以 JSON 格式从drupal 6站点提取数据,然后在 iphone 应用程序中解析 JSON 数据。

我想显示一个特定的节点,然后是它的所有注释。节点 ID 将由 iphone 应用程序提供。 我不能使用 REST 服务器来做到这一点...如果我将 url 指定为

<drupal site>/<REST server endpoint>/node/<node id>.json

然后我得到关于节点的所有相关信息,除了评论。

我不能使用drupal 视图来做到这一点……因为我只能显示一条评论……不是所有评论。此外,使用视图我必须指定节点 ID。

如何实现我的目标?

4

2 回答 2

0

如果没有直接的方法可以通过服务模块获取它,那么有一种方法可以通过创建您自己的自定义模块并实现 hook_services_resources 来获取节点及其注释。这种钩子机制类似于 drupal 菜单钩子。

这是用 drupal 7 编写的代码片段,但它也应该在 drupal 6 中工作。

Url : <drupal site>/<REST server endpoint>/my_rest_api/<node id>.json

让我看看它是否有效,否则我将在 D6 中创建整个模块并将其粘贴到此处。

 /**
 * Implements hook_services_resources().
 * my_rest_api should appear in your resource list and do enable it before using it.
 */
function YOURMODULE_services_resources() {
  return array(
    'my_rest_api' => array(
      'retrieve' => array(
        'callback' => 'getMyRestNodeWithComments',
        'args' => array(
          array(
            'name' => 'nid',
            'optional' => FALSE,
            'source' => array('path' => 0),
            'type' => 'int',
          ),
        ),
        'access callback' => 'getMyRestAcces',
      ),


    ),
  );
}

/**
 * Get the node along with comment
 */
function getMyRestNodeWithComments($nid) {
    $node = node_load($nid);
    $node->comments = getMyRestCommentByNid($nid);
    return $node;

}

/**
 * Access callback.
 * TRUE for now but you change it according to your requirement
 */
function getMyRestAcces() {
  return TRUE;
}
/**
 * Get comment by nid
 */
function getMyRestCommentByNid($nid){
    //drupal 7
    $query = db_select('comment', 'c');
    $comments = $query
        ->fields('c')
        ->condition('c.nid', $nid)
        ->execute()
        ->fetchAll();
    return $comments;

    /*
    //In Drupal 6 something like this 
    $result = db_query("select * from {comment} where nid = %d",$nid);
    $records = array();
    while($row = db_fetch_array($result)){
        array_push($records, $row);
    }
    return $records; 
    */

}
于 2012-09-23T08:10:16.243 回答
0

为此,我开始开发一个模块......我还没有包含评论,但该框架可能是一个很好的起点。

http://drupalcode.org/sandbox/nally/1365370.git/tree

于 2012-09-22T22:13:24.733 回答