Hybrid Hacking

Subscribe

 

Hybrid Hacking provides tutorials on how to integrate with some of today's hottest web services.

 

 

Hybrid Hacking

 

Links

RIApedia
Ryan Stewart
AllFacebook
TechCrunch

 BlogTutorialsAbout 

Saving CCK and Taxonomy Fields with Flex and Drupal Services

The Services module of Drupal lets you easily access the backend data of your Drupal-based web site or application. Flex can connect to these services via AMFPHP and gives you the ability to quickly build a nice front-end to your application that calls remote methods to get at the application data.

This post is aimed at developers already familiar with how to save nodes with Flex and Drupal services. If you don’t know how, but are interested, check out the tutorials, screencasts, and examples in the Drupal Services Handbook.

One of the services available allows you to save a new node to the database. Saving a basic node is quite straightforward, but adding in other fields like taxonomy terms and Content Construction Kit (CCK) fields isn’t immediately obvious.

Saving CCK Text Fields

When you are building the node object that will be passed to node.save, you add a property in the format field_XX where XX is the name of your CCK field. Then the value of that property should be set to equal an array structure. Here is some sample Flex code using my CCK field “video”:

...
var nodeObject:Object = new Object();

nodeObject.type = "page";
nodeObject.name = username.text;
nodeObject.title = title.text;
nodeObject.body = body.text;
nodeObject.field_video = new Array({value:"http://www.youtube.com/"});
node.save(nodeObject);
...

Saving Taxonomy Terms

To save a list of taxonomy terms the process is similar. Build an array structure that will hold the taxonomy terms and then assign that array to the taxonomy property of the node object you are passing into node.save. Here is the sample Flex code:

...
var taxonomyArray:Array = new Array();

// set the 'technology' vocabulary to have the terms with id 1 and 2
taxonomyArray['technology'] = new Array(1,2);

// set the 'industry' vocabulary to have the terms with id 3 and 4
taxonomyArray['industry'] = new Array(3,4);

nodeObject.taxonomy = taxonomyArray;
node.save(nodeObject);
...

Comments

Typo: "nodeObject.taxonomy = node.save(nodeObject);"

Thanks RobRoy I fixed that typo.

Thanks this has been a good help to me.

However, I am now stumped with the CCK Image Field. Although I can upload an image to the server, I cannot populate the image field itself. Any ideas?

Hi! A little late I know but any help would be appreciated. I have recently been playing with Drupal and Flex as a combination and have hit a stumbling block with CCK. I figured much of the above before finding this blog post but every time I try to save a CCK node I get the error: "array_merge(): Argument 1 is not an array". Do you have any ideas as to what may be causing this?

Well this looks great but it's not working for me.
Perhaps it's because I'm using a freetaging vocabulary and I need to send a string in the form term,term,term at some point in the object structure that I just can't quite get.

But one thing struck me as odd, you define taxonomyArray as an array, but then you treat it like an Object taxonomyArray['technology']

Thanks so much for sharing - I have been killing myself trying to figure this out

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
 

Copyright © 2007 Steven Shongrunden