An example of how you can use custom tasks in sequences is demonstrated by the use of some Tasks in the DrupalService. This post will demonstrate how two very easy to create Task subclasses give you a general solution to handling a specific dataflow from Drupal. We will show you how you can abstract the repetitive flow of connecting to a drupal backend and getting a specific set of nodes from a certain view.
The normal flow of interacting with a drupal backend is by first connecting with the system to get a session id, this is advised as it gives better security handling. When you have the session id, you would want to retrieve data from the nodes in Drupal. After you have all the node data you want to manipulate it, for instance, generate a view where you can access all the nodes’ data.
The previous paragraph has “Sequence” written all over it, so we will use our very powerful Task based Seqence managing package to abstract away all the low level details of interacting with Drupal, to be able to quickly get our data without writing a lot of communication code.
//the sequence that will load our data from drupal private var sequence : Sequence; //a list that holds nodes private var nodes : List = new LinkedList(); public function main() : void { //a drupalService that needs a session for security. var service : DrupalService = new DrupalService("http://www.example.com/amfphp/gateway.php", true); sequence = new Sequence(); //because we need a session, we first must connect. create a task to connect before getting any node data sequence.add(new DrupalConnectTask(service)); //now get some node data! in this case nodes with id 1 to 100 and use the myNodeHandler method to handle the incoming node data. for(var i : int = 1;i < = 100; ++i) { sequence.add(new DrupalNodeTask(service, i, myNodeHandler)); } //add callback task to sequence of tasks to process all the nodes sequence.add(new CallBackTask(allNodesLoaded)); //start the sequence. sequence.execute(); } /** * callback method specified in the DrupalNodeTask constructor. * It takes a DrupalNode object as it's first argument. * The node is passed in via the DrupalNodeTask */ public function myNodeHandler(node : Object) : void { //we handle the node here, in this case, we just put it in a list for later manipulation nodes.add(node); } /** * the last task in the sequence is a CallBackTask which just calls this method. * We will handle the end of the sequence here and manipulate the nodes we have retrieved from the backend. */ public function allNodesLoaded() : void { //we can now manipulate all the nodes in the "nodes" LinkedList. //for example, put all the data in Domain Objects and generate a view from it, //like a list where you can click on the title to get the node data etc. }
The previous code has shown how simple it now is to get data from drupal and manipulate it and how we totally abstracted away all low level drupal connecting code. You can check out the DrupalNodeTask and DrupalConnectTask to see how simple it is to set up a Task specific for your wishes.
There also is a DrupalViewTask that works exactly the same as the DrupalNodeTask, except that it takes a view name as an argument instead of a nodeId and the callback method will handle the data which came back from the view request which is a multidimensional array with possible multiple nodeId’s or node data. For more information on this, you can also read the comments in the earlier DrupalService post where there is a question about this.
It is not only easy to implement, but also easy to read as code. All the tasks are manipulated in separate Callback tasks…Nice, because I don’t wanna use events. Greate job
Here’s my first attempt to convert my Drupal portfolio into flex :
http://www.brandnewside.com
Sorry, the content is in French, translation to English is on the way. But amateurish developer like me can still look at the source : comments are in English… well, sort of…
Professional developer : you’ve been warned.
Anyway, thank you again, your stuff works perfectly.
Hi Benoit,
veryyy nice.. Also very nice to see that you actually used the package as envisioned by us (thanks to letting everyone be able to view your source code) and in the way we use it ourselves. Subclassing the Task class to create powerfull sequences etc. Nice to hear you could really use it and thanks for letting us know
take care