Community Documentation

JavaScript - Node.Save & Edit // Cross-method Insights

Last updated May 20, 2010. Created by yajnin on April 17, 2010.
Edited by heyrocker. Log in to edit this page.

// #### JavaScript - Node.Save & Edit // Cross-method Insights

// ## For EDIT info refer to the code comments in node_object

// Releases
// // # SERVICES 6.x-2.0 and JSON SERVER 6.x-2.0-alpha1

// Most of this would apply to older versions as well

// // If your Services server uses the drupal_to_js() method
   // // Or if you are generally having problems with processing your node object
   // // You may need to serialize (make a text string) your node object, or other deep structs / arrays
   // // I've made notes below in "**Serialization Issues**"

// + + Node Object

var date = new Date();
var timestamp = Math.round(date.getTime() / 1000);

var node_object = {
    "type": "story",
    "nid": 0,  // Set value to 0 for node.save. Use nid (int) for node.update
    "uid": 1, 
    "name": "admin",
    "title": "My First Node",
    "body": "Hello World!",
    "created": timestamp, // Set key to 'created' for node.save. Use 'changed' for node.update
    "status": 1 // Set to 1 for Published. O for unpublished.
};

  // **Serialization Issues**

  // Some Services servers (such as older versions of the JSON Server module, SWX PHP) have poor parsing capabilities that fail on deep structs or arrays
  // You may get a hair-pulling mystery error that boils down to one simple solution:
  // // You need to convert your node_object to a JSON or Serialized PHP string before calling the method

  // For example, with JSON:
  // // var object = { "foo": "bar" };
  // // Should be:  var object_string = "{\"foo\":\"bar\"}';

  // To do this automatically, using MooTools, I'd use this method:

  // // node_object = JSON.encode(node_object);

  // If you're using Flash or working with Serialized PHP, I recommend reviewing this thread
  // // http://groups.drupal.org/node/66478

// CCK Fields

// Reference: JavaScript - CCK Fields in the Node Object (applies to any language)
// http://drupal.org/node/776122

// + + Data Object

// This data object is the instruction between your Drupal Services Server
// // Example of how to implement this is in the next code box below

var data: {
  "method": "node.save",
  "node": node_object
};

// #### Calling the Request

// The Following examples are for MooTools
// // Whether you use jQuery or Flash/Flex, etc, you'll have similiar methods

JSON
http://drupal.org/node/522942

JSONP
http://drupal.org/node/791922

// #### Reference: Services & JavaScript

JavaScript - Node.Save & Edit // Cross-method Insights
http://drupal.org/node/774116

JavaScript - CCK Fields in the Node Object // Cross-method Insights
http://drupal.org/node/776122

JavaScript - Comment.Save
http://drupal.org/node/788892

JavaScript - JSON Server // Request.JSON (MooTools Example)
http://drupal.org/node/522942

JavaScript - JSON Server // Request.JSONP (MooTools Example)
http://drupal.org/node/791922

Comments

line 170 or so of node_service.inc

If you're using the JSON_SERVER (maybe not even that) and have some custom CCK fields, you might want to do something like this. It works fine for basic fields... not sure about more complex ones.

<?php
    $node
= new stdClass();
   
$node->type         = $edit[type];
   
$node->nid          = $edit[nid];
   
$node->title        = $edit[title];
   
$node->body         = $edit[body];
   
$node->uid          = $edit[uid];
   
$node->status       = 1;
    foreach (
$edit as $key => $value) {
      if (
substr($key, 0, 6) == "field_") {
       
$node->{$key}[0]['value'] = $value[0]->value;
      }
    }

   
// Setup form_state
   
$form_state = array();
   
$form_state['values'] = (array)$node;
   
$form_state['values']['op'] = t('Save');

   
$ret = drupal_execute($node->type .'_node_form', $form_state, $node);
?>

Sudo bring me the remote. Drupal Web Design

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here