Wow, life can be whacky.
So I am pretty sold on Drupal Gardens as a base for most of my future development projects. However, there are still a couple of useful pieces missing, that are out-of-the-box available on a full Drupal install. Specifically, the Services module. Services allow alternate means of accessing data stored in the Drupal database. So if I want to, say, use Drupal as a content management system for a Flash game, I can just pull information in as and when I need it, by referencing a URL and passing the appropriate parameters.
Drupal Gardens doesn’t allow that yet. Well, technically they do, but only in very specific ways, none of which are ideal for using Drupal as a straightforward CMS. Having said that, apparently adding this functionality is something they are looking into.
But enough carping! It is possible to use DG as a CMS for a Flash (or AJAX, or Silverlight, etc) application, provided you only need to pull content out of the database, not put it back in. It just takes a bit of a work-around to get everything up and running.
Note: The rest of this post assumes a familiarity with Actionscript, Drupal Views and RSS feeds. If not, take a few minutes to read up on them.
The Views module allows the contents of a view to be published as a Page, as a Block, or as an RSS feed. Say I am making a role-playing game, and I need to populate the world therein with wildlife. To keep things simple, each critter has the following information points:
- name
- description
- terrain (where the creature might be encountered)
- associated element ( from the classic 5, for combat purposes)
So after entering data for several animals, you would end up with a table which looks something like this:
Name | Description | Terrain | element |
---|---|---|---|
squirrel | cute, fluffy, voracious | forest | wood |
camel | cute, fluffy, spits a lot | desert | water |
walrus | truly, nature’s most majestic animal | plains | earth |
…at least, that’s how it looks in the database. To get it into Flash (in Drupal Gardens, at present) requires a little more work. But not a lot more work.
Pulling the RSS feed of a view is quite simple. Just create a “feed” version of an existing view, and set up a URL path for it, and voila! You have a feed of the contents of a view.
However, note that the actual contents of a view, when delivered in an RSS feed, are all packed into the <description> tag of the view, and include all of the HTML which would normally be rendered in the page. It looks something like this:
http://ecceludum.drupalgardens.com/feeds/monsters/terrain/desert en http://ecceludum.drupalgardens.com/content/camel <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even" property="content:encoded"> <p>camel text</p> </div> </div> </div> <div class="field field-name-field-monster-element field-type-taxonomy-term-reference field-label-above"> <div class="field-label">element: </div> <div class="field-items"> <div class="field-item even"> <a href="/elements/water" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Water </a> </div> </div> </div> <div class="field field-name-field-monster-terrain field-type-taxonomy-term-reference field-label-above"> <div class="field-label">terrain:&nbsp; </div> <div class="field-items"> <div class="field-item even"> <a href="/terrain/desert" typeof="skos:Concept" property="rdfs:label skos:prefLabel">desert </a> </div> </div> </div> Thu, 15 Dec 2011 18:22:47 +0000 John Winkelman 71 at http://ecceludum.drupalgardens.com http://ecceludum.drupalgardens.com/content/camel#comments
All well and good, except boy, are the contents of the DESCRIPTION tag ugly. That is because the angle brackets, ampersands, quote marks and non-breaking spaces have been re-written as plain text character entities. In a sense, they have been rendered as a picture of source code, rather than source code. There is a simple fix for this: When Flash pulls in an RSS feed (or any other XML-based document) it pulls this content in as plain text. It is not structured as XML until after it is loaded into the Flash movie. This means that the string can be parsed internally, and all of the character entities turned into their respective text elements. E.g. each instance of “>” can be replaced with a “>”. The simplest way might be the following line of code:
var newString = oldString.split("<").join("<").split(">").join(">").split(""").join("\"").split(" ").join(" "); var myXML = new XML(newString);
If that is run on the preceding big ugly bit of RSS feed, then the contents of the DESCRIPTION tags would suddenly look like this:
<div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even" property="content:encoded"> <p>camel text</p> </div> </div> </div> <div class="field field-name-field-monster-element field-type-taxonomy-term-reference field-label-above"> <div class="field-label">element: </div> <div class="field-items"> <div class="field-item even"> <a href="/elements/water" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Water </a> </div> </div> </div> <div class="field field-name-field-monster-terrain field-type-taxonomy-term-reference field-label-above"> <div class="field-label">terrain: </div> <div class="field-items"> <div class="field-item even"> <a href="/terrain/desert" typeof="skos:Concept" property="rdfs:label skos:prefLabel">desert </a> </div> </div> </div>
Now the contents of the DESCRIPTION element of the RSS feed XML are structured and can be parsed using the Flash XML tools. In this instance, you would look for the contents of the elements div.field-name-field-monster-element a, div.field-name-field-monster-terrain a, and div.field-name-body p.
You will note that there are a lot of extra DIV tags, and many of them have huge long class names and/ir IDs. This is not a problem for two reasons: first, the data is well-structured, and second, it is consistent. Unless there are changes to the structure of the View in Drupal Gardens – e.g. changing the “terrain” data field to be called “territory” instead – every time the data is pulled from this RSS feed for this View, it will have exactly the same structure. Having five records or ten thousand won’t change things.
So there it is: An overview of how to access Views information from a Flash movie, via RSS, in Drupal Gardens.