Example: Saving a node using Adobe Flex

Last modified: September 24, 2007 - 08:00

This code sample demonstrates the simplest way of saving a node using Adobe Flex and Drupal Services.

Note: You must be signed into Drupal through your browser as administrator for this example to work properly.

First ensure you have Drupal installed with the Services and AMFPHP modules properly set up. Refer to this page for more details on how to do this.

Next copy the following MXML code into a new Flex Project and then update the endpoint to point to your AMFPHP gateway:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="185" width="256">
  <mx:Script>
    <![CDATA[

      import mx.controls.*;
      import mx.rpc.events.*;

      public function onFault(event:FaultEvent):void{
        Alert.show(event.fault.faultString, "Error");
      }

      public function nodeSaveResult(event:ResultEvent):void{
        mx.controls.Alert.show("Node saved", "Success");
      }

      public function saveNode():void{

        var nodeObject:Object = new Object();

        nodeObject.type = "page";
        nodeObject.nid = nid.text; // set to zero to create new node
        nodeObject.title = title.text;
        nodeObject.body = body.text;

        node.save(nodeObject);

      }

    ]]>
  </mx:Script>

  <mx:RemoteObject endpoint="http://localhost/drupal/services/amfphp" showBusyCursor="true" destination="amfphp" source="node" id="node">
    <mx:method name="save" result="nodeSaveResult(event)" fault="onFault(event)"/>
  </mx:RemoteObject>

  <mx:Label x="10" y="10" text="Save Node" fontWeight="bold"/>
  <mx:Label x="10" y="38" text="Node ID:"/>
  <mx:Label x="10" y="65" text="Title:"/>
  <mx:Label x="10" y="91" text="Body:"/>

  <mx:TextArea x="84" y="64" width="161" height="19" id="title"/>
  <mx:TextArea x="84" y="90" width="161" height="51" id="body"/>
  <mx:TextInput x="84" y="36" text="0" width="44" id="nid"/>

  <mx:Button x="159" y="149" label="Save Node" click="saveNode()" />

</mx:Application>

Finally, run the Flex application, type in a title and body, then click "Save Node."

You can confirm that the node was saved by checking Drupal in your browser.

this example works with a

psi-borg - November 12, 2007 - 11:48

this example works with a few fixes:

public function saveNode() ...should include the statements (statements to set the nodeObject.changed value)

var my_date:Date = new Date();
nodeObject.changed = my_date.getTime(); //Upon update, node object must include "nid" and "changed".

i found i wasn't able to add a node through this flex app, even though i received a "Success" result... and updating a node returned the error "This content has been modified by another user, changes cannot be saved."

{drupal}/admin/build/services/browse/node.save ...indicates

Upon update, node object must include "nid" and "changed".

but apparently, the "changed" timestamp must also be included upon node creation. i could see no reason why the "changed" field wasn't given a default of now() for the timestamp in every case.

Error: class {node} could not be found

Herre_84 - November 28, 2007 - 10:31

Hi,

I'm getting following error when running your example:

The class {node} could not be found under the class path <code>{C:\xampp\htdocs\drupal\modules\services\services\node_service\/node.php}

<code>AMFPHP_FILE_NOT_FOUND

<code>C:\xampp\htdocs\drupal\modules\amfphp\amfphp\core\shared\app\BasicActions.php on line 33

I used the gateway.php as an endpoint, but it seems to look for a .php file, while all services are .module files... Do I have to write a custom php file, from which I have to refer to the module? Isn't it possible to make use of the modele file directely?

Hope you can help,

Thanks in advance, Hans

I think the screencast

styleplus - January 5, 2008 - 19:05

I think the screencast tutorials found here may be more helpful to you all:
http://drupal.org/node/144141

I have actually followed through the steps one time ago and was able to load and save nodes within a small Flex 2 application. Check it out.

New Node Creation Fix

z3cka - March 18, 2009 - 07:59

for new node creation be sure to use:

nodeObject.created = my_date.getTime();

I was not able to find this in the api documentation... I guessed, and it worked. what page can i add this to?

CCK fields

jacopo3001 - April 9, 2009 - 11:52

great samples, it works perfectly. Just pay attention to nodeObject.created, as stated already.
In my case my_date.getTime(); did not work, i had to use normal format (eg: "2009-1-14")
Besides this, its great.

now i have another problem: in all samples i found, we always just interact with Body, nid or title.
what if i have to save a field that was created with CCK?

i am trying as
myNodeObject.field_text
and i always get error: "Channel disconnected before an acknowledgement was received", and of course the node is not saved... :(

any idea?
i cannot find any documentation on how services manages data and functions...

answer to myself

jacopo3001 - April 12, 2009 - 05:00

this syntax to edit CCK created text fields

nodeObject.customField = new Array ({value:"someText"});

it works!

Thank you, and re: CCKs

SCaV3NG3R - June 17, 2009 - 12:11

Your comments to use nodeObject.created when new, and nodeObject.changed when updating, with a particular format of "yyyy-mm-dd" were lifesaving to get past the error message, but it seems drupal needs the hours and minutes too, because in my case it time stamped them "2009-06-16" into "1969-12-31 20:33:29 -0400"...

Anyway, regarding CCKs, this works for me:

When reading a node:

myfield=resulting.result["field_myfield"][0].value;

and when writing that note back:
nodeObject.field_myfield=[{format: "3", value: myfield}];

The code above assumes your read function captures the return in a var call "resulting", and "format: "3"" means text field, for CCKs.

Hope this is useful to someone :)

 
 

Drupal is a registered trademark of Dries Buytaert.