Example: Saving a node using Adobe Flex

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.

 
 

Drupal is a registered trademark of Dries Buytaert.