Search code examples
phpdrupalpostmandrupal-7

Drupal 7 Services create node with images


I installed a new Drupal 7.56 distribution. I want to create new nodes via java client. However, After little googling I found the “Services” module for Drupal 7.

So I installed it, with the next module:

  • Services
  • Ctools (required for Services)
  • Libraries (required for Services)
  • OAuth 1.0

So, I installed these modules. From the Structure menu I created the Service.

  • Endpoint: API
  • Server: REST

I enabled the Session and OAuth authentication.

I created a new Content type. name: TestContent (Machine name: testcontent) Fields:

  • Title (M.n.: title)
  • Body (M.n.: body)
  • Pics (M.n: field_pics) (Type: Image) Number of values: 5

In this Service I enabled all resource (file, user, etc..) I Disabled the OAuth, because I will set up it later.

Now, I opened my Postman client. Logging in: admin/admin

{
    "sessid": "QZTYSQu3-I9pacOpoSP--V_LkGcusy2grl12U_CyKrY",
    "session_name": "SESS51ebf8732a20744576a234cf7af43040",
    "token": "jkUDb6MsGMHt_mBlGbm02O-lyZq-2nRTqD1OslxtvAg",
    "user": {
        "uid": "6",
        "name": "admin",
…

Now I have a token. Now I upload two picture.

http://test.dd:8083/api/file

Got these responses

{
    "fid": "6",
    "uri": "http://test.dd:8083/api/file/6"
}
{
    "fid": “7”,
    "uri": "http://test.dd:8083/api/file/7”
}

Ok, Now I’ll try to create a new TestContent, and connect these Images to the node.

enter image description here

Ok, The node is created. But the Images isn’t connected to the node. But I didn’t get error message. Why? What’s wrong?

I tried:

[ {fid:6} , {fid:7}]
und: [ { fid: 6 }]

Please give me ideas. Thank you


Solution

  • I found the solution on the Drupal site.

    Run these GIT difference!

    diff --git a/resources/node_resource.inc b/resources/node_resource.inc
    index 8f870b7..6669a1a 100644
    --- a/resources/node_resource.inc
    +++ b/resources/node_resource.inc
    @@ -339,8 +339,9 @@ function _node_resource_create($node) {
       );
       $stub_form = drupal_build_form($node_type . '_node_form', $stub_form_state);
       $form_state['triggering_element'] = $stub_form['actions']['submit'];
    +  $node = (object) array_merge((array) $stub_node, (array) $node);
    
    -  drupal_form_submit($node_type . '_node_form', $form_state, (object)$stub_node);
    +  drupal_form_submit($node_type . '_node_form', $form_state, $node);
    
       if ($errors = form_get_errors()) {
         return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
    @@ -423,6 +424,7 @@ function _node_resource_update($nid, $node) {
    
       $node_type = $node['type'];
       node_object_prepare($old_node);
    +  $old_node = (object) array_merge((array) $old_node, (array) $node);
    
       // Setup form_state.
       $form_state = array();
    diff --git a/resources/user_resource.inc b/resources/user_resource.inc
    index 304a293..c1f9b5a 100644
    --- a/resources/user_resource.inc
    +++ b/resources/user_resource.inc
    @@ -471,6 +471,7 @@ function _user_resource_update($uid, $account) {
         return services_error(t('You are not allowed to change your username.'), 406);
       }
    
    +  $account_loaded = (object) array_merge((array) $account_loaded, (array) $account);
       $form_state['values']['op'] = variable_get('services_user_save_button_resource_update', t('Save'));
       $form_state['values']['#user_category'] = $category;
       $form_state['values']['#account'] = $account_loaded;
    

    See more: Here