This post is part of a series of where I present a solution that allows an easy integration of Rich Internet Applications in Drupal 7. I have already set the requirements of this project in the previous post : Rich Internet Applications / Drupal Integration Project Requirements
This entry describes the RIA module. This module is responsible of loading all the JavaScript and CSS resources needed by a Rich Internet Application.
For the sake of completeness, the INFO file of the RIA module is listed below
- name = RIA
- description = Easy Integration of Rich Internet Applications with Drupal.
- core = 7.x
- package = My Modules
- configure = admin/config/content/ria
RIA module implements a single Drupal hook, namely hook_node_view(). This hook is invoked by the Drupal core before a node is rendered. The implementation of hook_node() is depicted below. RIA resources should only be loaded when the node corresponding to the application's page is viewed in full. For instance, we do not want to load such resources when someone visits the homepage of our site and if this home page contains say a dozen of RIA 's. This check is done in line 11. Afterwards, the function looks if the current node has an field_ria_application_directory property. Application resources are supposed to be stored under directory sites/all/modules/ria/
- /**
- * @files
- * Allows easy integration of Rich Internet Applications with Drupal
- *
- * @author Skander Kort
- * @copy This code can be used in any project as long as you include this docblock
- */
- function ria_node_view($node, $view_mode, $langcode) {
- if ('full' == $view_mode) {
- $application_directory = drupal_get_path('module', 'ria') .
- DIRECTORY_SEPARATOR .
- $node->field_ria_application_directory[LANGUAGE_NONE][0]['value'];
- _ria_add_css($node, $application_directory);
- _ria_add_api_js($node);
- _ria_add_application_js($node, $application_directory);
- _ria_add_inline_js($node);
- }
- }
- }
- }
Function _ria_add_css() loads the application's CSS file. The CSS file name is given by property field_ria_css. This file should be stored under directory sites/all/modules/ria/field_ria_application_directory/css. The markup required to load this file is added by function drupal_add_css().
- function _ria_add_css($node, $application_directory) {
- $css_file = $application_directory . DIRECTORY_SEPARATOR .
- 'css' . DIRECTORY_SEPARATOR . $node->field_ria_css[LANGUAGE_NONE][0]['value'];
- drupal_add_css($css_file);
- }
- }
Function _ria_add_api_js() loads the code of any external JavaScript libraries needed by the Rich Internet Application. The URLs of these libraries are stored as an array in node property field_ria_javascript_api. The markup to load the JavaScript code is rendered by function drupal_add_js(). We specify that the API's URL is that of an JS_LIBRARY. This tells Drupal to load this library before any custom JavaScript (that might rely on it).
- function _ria_add_api_js($node) {
- foreach ($node->field_ria_javascript_api[LANGUAGE_NONE] as $url) {
- }
- }
- }
The JavaScript of the application is held in one file. The name of this file is given by node property field_ria_application_javascript. This file should be stored under directory sites/all/modules/ria/field_ria_application_directory/js.
- function _ria_add_application_js($node, $application_directory) {
- $js_file = $application_directory . DIRECTORY_SEPARATOR .
- 'js' . DIRECTORY_SEPARATOR . $node->field_ria_application_javascript[LANGUAGE_NONE][0]['value'];
- drupal_add_js($js_file);
- }
- }
Finally function _ria_add_inline_js() allows adding any inline JavaScript. This is added to the head element of the RIA's page. The inline JS is stored in node property field_ria_inline_javascript, without the script tags.
- function _ria_add_inline_js($node) {
- $js = $node->field_ria_inline_javascript[LANGUAGE_NONE][0]['value'];
- drupal_add_js($js, 'inline');
- }
- }
You can now create a new content type for Rich Internet Applications through Drupal's user interface. The new content type must have all the fields required by RIA module. I will describe in the coming post the installation hook that will save you this trouble.