Skip to Content

calling drupal functions

Background:

Once upon a time, connecting Flash content with a database backend involved writing php pages and calling them from Flash. Over the years, the method for calling them evolved somewhat, but prior to AS3, things stabilized around the excellent LoadVars object. Within your php page, you would connect to the database, run queries, and return the results to Flash formatted as a GET string.

While this was a reasonably straightforward arrangement, you did have to establish a database, worry about the security of your database, establish database connections, think about sessions, etc.

Now Drupal is my backend

Putting Flash inside of Drupal solves one batch of problems and creates another. You can write a Drupal module that will create your database table, establish a database connection, and help you write safe queries, but it raises a difficult question: from within Flash, what url can you provide for the LoadVars.sendAndLoad function call?

First try

The first thing I tried was writing Drupalish code and placing it in a standalone php file, which I called from Flash. Following some advice I found at http://www.travistidwell.com/drupal_flash_interface, I attempted to bootstrap the Drupal core functions into my page. This was a partial success, but did not work well from within a subdomain. Bootstrapping turned out to be a nightmare, and besides, I knew in my heart that a standalone bootstrapped php file was not truly the Drupal Way.

Second Try

My second try was copied from a module a colleague suggested. Basically it boiled down to putting a standalone file into a module folder. This turned out to be an even more dismal failure.

Third try

My big break came when I decided to try to hack my way into the database by way of a hidden Drupal form. This actually worked out quite well. I could put a form into a hidden div, and then populate its fields from Flash using javascript. Pretty cool! The problem, as well as the breakthrough, came when I tried to submit the form without causing a page refresh. While searching desperately for a way to initiate an AJAX form submission, I eventually stumbled across the documentation not only for AHAH (like AJAX, but without the XML) form submission, but also for the all important hook_menu.

Using both hook_menu and AHAH, I did manage to send data to the db from Flash without refreshing the page, but I also started to suspect that hook_menu might have been all I ever really needed.

Calling functions directly

It took some experimentation to get the syntax right, but here is what is working for me today:

  1. In a .module page, write a function that does pretty much exactly what you would have written in a standalone php page, but take advantage of all those slick Drupal functions. Instead of a return, write out Flash variables using a print statement.

    function e_page_js($nid,$earned) {
    global $user;

    $data = array(
    'timestamp' => time(),
    'uid' => $user->uid,
    'nid' => $nid,
    'earned' => $earned
    );
    $update = array('uid','nid');
    $result = drupal_write_record('e_page_grade',$data,$update);

    if(!$result){
    print "reply=There has been a problem recording this score";
    }else{
    print "reply=Your score has been recorded&";
    }

    }

  2. Still in the .module page, write a hook_menu item pointing a url path to your function. I won't go into details, because the documentation is decent, once you find it.

    function e_page_menu() {
    $items = array();
    $items['e_page_js/%/%'] = array(
    'page callback' => 'e_page_js',
    'page arguments' => array(1,2),
    'access arguments' => array('access e_page js'),
    'type' => MENU_CALLBACK,
    );

    return $items;
    }

  3. In Flash, package your data as Drupal-style url "arguments". The arguments look like part of the url path, so you won't have any traditional LoadVars properties containing data. Everything goes into the path. I am using LoadVars.sendAndLoad even when I do not expect a return. I haven't experimented much with LoadVars.send, maybe that works just as well. I have played with getURL, which at first seemed to work, but then later I was mysteriously leaving my original page and going to the pseudo-page suggested by the url. Since I didn't know what had happened to change the outcome, I switched to sendAndLoad figuring that was the surest way to actually stay on the page containing the Flash content.

    getData.sendAndLoad("e_page_js/"+nodenum+"/"+earned,catchData,"POST");