Introduction
Oh, on a side note, good luck trying to find this information anywhere else on the Internet. I tried but didn't succeed. So instead of just giving up, I actually brainstormed on a method of actually doing this. So, in this tutorial, I will explain to you the easiest way to happily marry Flash applets with a Drupal website. So, let us begin.
Understand how Flash extracts data
Flash receives outside data through the use of parameters passed to the Flash applet. There are two ways to pass parameters to your Flash applet. You can either use the $_GET method where you just specify parameters as you would in a URL like flashapplet.swf?param1=2¶m2=3, or by just using the parameter called FlashVars. FlashVars works very similar to that of the $_GET method and is actually the preferred method for sending data to your flash application. You do this by specifying a new parameter in the object code of your Flash object. It should look like the following.
<param name="FlashVars" value="param1=a¶m2=b¶m3=c" />
Note that each parameter passed using FlashVars are separated by an "&". Flash will then parse each one of these parameters and actually create a variable within the ActionScript so that you can use it willingly. However, using this method, you must specify every single parameter separately and within the actual object code. Do you see the problem? What if you wanted to pass a whole lot of data to the Flash applet? Well, in response to this, Flash has also added the ability to extract parameter information from external files. You actually open the file and extract the file information within the ActionScript of the Flash applet (which is beyond the scope of this tutorial), but a person could easily specify WHICH file to open by using this FlashVars parameter like the following.
<param name="FlashVars" value="file=external_file.txt" />
Now this file can be in the form of an XML file (if you wish to do XML parsing), a text file, or even better a URL... Hmmmmmm... Now were onto something. If I can specify a URL, then that would mean that I could also specify a PHP file, and then in the PHP file just echo any of the parameters to the Flash applet (XML format or whatever else)!! Now, that's cool!!!... unless your using Drupal.
The problems with Drupal
Drupal, if you haven't figured this out already, has a ninja death grip on all database extraction. What I basically mean by this, is the only way to truly extract any database information from Drupal is do so through the Menu / Path system of Drupal. This is just crappy for me, because this means that I can't specify a file that just gives me the raw data and nothing else!!! This is a great thing for web security, but painful if you wish to have a Flash applet extract information from your Drupal database. This is what took me forever to figure out how to bypass. I tried everything from creating a separate template file... actually creating a file and using external database modules...you name it. I just couldn't figure out how to get all the raw data without Drupal adding all that template HTML crap along with it. Well, after much learning and studying of the Drupal system, I finally figured out the easiest way to do this. I basically asked myself "When I go to any Drupal URL, where is it actually going?". And the answer was simple... index.php. All the magic of Drupal happens in this extremely small file. I know it is hard to believe, but it does. When you go to ANY url in the Drupal system, you are actually going to www.travistidwell.com/index.php and then anything else is just passed as an argument to this file. This is actually brilliant on Drupal's part. So, how do you extract information from your Drupal database with a PHP file... well, create your own version of the index.php file, thats how! For this tutorial, we will call it flash_interface.php.
Create your very own flash_interface.php file
The first thing that we need to do is create a new file called flash_interface.php and place that file in the root directory of Drupal (right next to the index.php file). The next thing that I am going to ask you to do is actually open up the index.php file. You can learn so much from these 40 lines of code. If you actually study the index.php file, you will notice two extremely important lines. The following code will be your template for creating an interface file.
<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?> These two little lines are all that it takes to basically "boot" Drupal. Think of it as a Windows operating system. The first thing that happens is your system boots up and loads windows. What does it do after that? Well, it starts loading applications. Well, in your flash_interface.php file, we want to boot Drupal, but don't want to start all the Menu / Path application stuff... so, we just won't include it. So in your flash_interface.php file, all you need to do is place those two lines at the very top of the file, and then you will have complete access to all the Drupal stuff!! One big thing is the database functions such as db_query will work as they normally would in a Drupal page or custom module. So, after those two lines you can put whatever you want, and echo whatever you want! What this means is that you can extract Drupal database information, and then just echo that information to your Flash applet! Here is an example...
<?php
/*
* Let us suppose that we want to create a Flash widget
* that shows how many nodes are in the system.
* This Flash applet will be simple in that all it expects
* as a parameter is the node_count variable.
*/
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // "boot" Drupal
// Here we extract the information from the database and echo
// that information in a Flash readable format.
$count = db_result(db_query("SELECT count(*) FROM {node}"));
echo 'node_count='.$count;
?> Pass your flash_interface.php file to the Flash applet
So, how does this all tie in with your Flash applet? Well, remember the FlashVars parameter? All you need to do is just specify the following parameter in your Flash object code.
<param name="FlashVars" value="file=http://www.travistidwell.com/flash_interface.php" />
What you can then do in your ActionScript for you Flash application is just open this as you would a normal file. To Flash, this would be the same as if you just opened a text file with all this information in it. However, since it is a PHP file, you are completely at liberty to make it DYNAMIC data using the Drupal Database. Now that's cool!!!
Take it one step further
Ok, now lets suppose that we wish to make a Drupal page that has a Flash widget right smack in the middle. All this widget wishes to do is show you the node information of the current page... Using this method, you could actually do this! Here's how... First, you will need to create a new page. In that page you will need to place the object code for your Flash widget. It should look simlar to the following.
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
width="320" height="240"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
<param name="movie" value="http://www.travistidwell.com/files/MyFlashWidget.swf" />
<param name="wmode" value="transparent" />
<param name="FlashVars"
value="file=http://www.travistidwell.com/flash_interface.php?nid=<?php print $node->nid ?>" />
<param name="quality" value="high" />
<embed allowScriptAccess="always" src="http://www.travistidwell.com/files/MyFlashWidget.swf"
width="320" height="240" border="0" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
wmode="transparent" quality="high"
flashvars="file=http://www.travistidwell.com/flash_interface.php?nid=<?php print $node->nid ?>" />
</object>Do you see what I did? I basically am now passing a $_GET variable to my PHP file that has the current node ID. With this information, I can construct my flash_interface.php file to look like the following.
<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // "boot" Drupal
// Here we will extract the node information from the Drupal database, and then echo that information
// in a Flash readable format.
if(isset($_GET['nid']) && is_numeric($_GET['nid'])) {
$node = db_fetch_object(db_query("SELECT * FROM {node} WHERE nid=%d", $_GET['nid']));
$params = 'nid='.$node->nid;
$params .= '&title='.$node->title;
echo $params;
}
?> What will happen now, is that the Flash widget in the middle of my node will now have information about that node!!! Now, how freakin cool is that!!!
Hope you enjoyed this tutorial...




Great Tutorial!
I love flash too... and always want to integrate it.
this was invaluable!
thanks
http://www.disorderdesign.com
Work with the latest versions of Drupal?
Man, i love that this exists, and I want to play, but.... the latest versions of Drupal are 5.x and 6.x(especially 5.x, as there seems to more support for it). You guys were using 4.x and... I'm a bit green. Any pointers for the new kids wanting to work with the latest versions of Drupal?
Menu callback
Travis...I'm doing a bit of research on this topic at the moment and stumbled across your site.
Why would you not stay closer to the way Drupal does things and just put your code into a module, establish a menu callback to a function that does what your file does and PRINT the results (rather than return, which will, as you mentioned, display a fully themed page).
function flashhandler_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'flashgetvars',
'type' => MENU_CALLBACK,
'callback' => 'flashhandler_process',
'access' => TRUE,
);
}
}
function flashhandler_process() {
// Here we will extract the node information from the Drupal database, and then echo that information
// in a Flash readable format.
if(isset($_GET['nid']) && is_numeric($_GET['nid'])) {
$node = db_fetch_object(db_query("SELECT * FROM {node} WHERE nid=%d", $_GET['nid']));
$params = 'nid='.$node->nid;
$params .= '&title='.$node->title;
print $params;
}
exit;
}
Btw...code formatting didn't work for me in the comment...
Ok sweet
I was thinking that you would say index.php... lol. Awesome tutorial. I think that i will be using that soon.
vague
sorry, can i ask what magic did you do in the flash file?
Ajax inside block implementation
Hello Travis,
I'm in this situation where I have a huge tree implemented inside a block. When the user clicks in any of the nodes (links) in the tree, I want the server to serve the page in the main content pane without refreshing the whole page ( you know with all the side bars, blocks) . Is this possible in Drupal? Also, I want to have a cookie implementation to remember the state of the tree.
Thanks
pol
Not working properly...
Thank you for this tutorial!
I tried everything you described but i can't get it working.
I changed your code a little because I only wanted the title of the page to be used in flash.
Everytime I trie it, I get the text undefined.
When I browse to "flash_interface.php?nid=7" the output is good.
In my Actionsscript i used the following:
var title:String;myTextField_txt.text = title;
The output from flash_interface.php is title=Thenodetitle, this looks ok.
But it still doesn't work, could you please help me?
Greetings,
Peter
Great technique
But doing a full drupal bootstrap takes a huge amount of time. Surely the point of this technique is speed, otherwise you could use a menu callback. Why not just:
include_once 'includes/bootstrap.inc';drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); // to access database
or "drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);" instead to access $user and database.
There is an interesting technique for drupal 4.6 described at http://docs.projectopus.com/releases/fastajax which allows for selected modules to be loaded. I'm not sure how to get it working in 5.1 without doing an almost full drupal boot; consequently it didn't really work any quicker than a menu callback. This might be time saving though for sites running lots of modules?
Great suggestion! I will
Great suggestion!
I will look into being more selective with my commands to the bootstrap routine. I guess it really depends on your requirements.
Thanks again,
Travis.
Hi Travis - you are right -
Hi Travis - you are right - there is not much anywhere about interfacing flash with drupal anywhere and for some reason - possibly due to my old software I couldn't get any of the above codes to work so I came up with this method witch you just write when creating a page in drupal in php mode.
<?php
global $user;
profile_load_profile($user->uid);
$getname=$user->name;
?>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0" width="751" height="451">
<param name="movie" value="http://pathtofile.com/files/displayname.swf<?php echo "?getname=$getname"; ?>" />
<param name="quality" value="high" />
<embed src="http://www.pathtofile.com/files/displayname.swf<?php echo "?getname=$getname"; ?>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="751" height="451"></embed></object>
Then in actionscript just use the variable getname at _root level
Good tutorial..was looking
Good tutorial..was looking for a guide about this.thanks