Drupal 7 开发内部培训资料, 分头诗人

service 模块,怎么在node里加入user的详细信息

赞成!
0
否决!

我在用户里添加了一个简介字段,然后在service里设置生成node json,生成的数据里边只有node对应的uid的name,没有我设置的字段,该怎么把我的用户的详细内容都加进json,比如我添加的简介字段,角色之类的?

3 个回答

赞成!
0
否决!

截图看看

赞成!
0
否决!

默认的nodejson里边只有node对应的uid的用户名,我想把所有的user的信息都加进去

赞成!
0
否决!

用了部分service的api,然后实现了,可能逻辑上有很多错误,但是,暂时解决了需求

/**
* service hook hook_services_resources()
*
*/
function pro_reservation_services_resources(){
    $resources = array();

    $resources['nodes']['operations']['retrieve'] = array(
        'help' => 'custom node retrieve',
        'callback' => '_node_services_resource_get',
        'args' => array(
            array(
                'name' => 'nid',
                'optional' => FALSE,
                'source' => array('path' => 0),
                'type' => 'int',
                'description' => 'The nid of the node to retrieve',
            ),
        ),
        'access callback' => '_node_resource_access',
        'access arguments' => array('view'),
        'access arguments append' => TRUE,
    );

return $resources;
}

function _node_services_resource_get($nid){

    if ($node) {
        $uri = entity_uri('node', $node);
        $node->path = url($uri['path'], array('absolute' => TRUE));
        unset($node->uri);
    }
    $uid =  $node->uid;

    $node = services_field_permissions_clean('view', 'node', $node);

    $account = user_load($uid);
    if (empty($account)) {
        return services_error(t('There is no user with ID @uid.', array('@uid' => $uid)), 404);
    }

    services_remove_user_data($account);

    //Lets check field_permissions
    $account = services_field_permissions_clean('view', 'user', $account);

    $user_key = array('uid','mail','name','created','access','login','picture','roles','data','body');
    $account = (array)$account;
    $node = (array)$node;
    $node['price'] = $node['field_price'];
    $node['image'] = $node['field_image'];
    array_unpacking($account,$user_key);
    array_unpacking($node);
    $info['node'] = $node;
    $info['user'] = $account;
    return $info;
}